日期:2014-05-17  浏览次数:21234 次

怎么在oracle建一个存储过程来遍历数组,新手求解
建一个存储过程
定义一个一维数组 Test{A,B,C,D,E,F,G,H,J,K,L}
怎么样用循环遍历数组,然后输出

新手求解

------解决方案--------------------
SQL code
create or replace procedure p_test as
type t_Test is table of varchar2(10);
test t_test:=t_test('A','B','C','D','E','F','G','H','J','K','L');
begin
for i in test.first .. test.last loop
dbms_output.put_line(test(i));
end loop;
end;
/
set serverout on
exec p_test;
---------------------
A
B
C
D
E
F
G
H
J
K
L
PL/SQL procedure successfully completed.

------解决方案--------------------
探讨

SQL code
create or replace procedure p_test as
type t_Test is table of varchar2(10);
test t_test:=t_test('A','B','C','D','E','F','G','H','J','K','L');
begin
for i in test.first .. test.last loop
dbm……

------解决方案--------------------
SQL code
DECLARE
  -- Define a varray of twelve strings.
  TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
   
  -- Define an associative array of strings.
  TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR)
    INDEX BY BINARY_INTEGER;

  -- Declare and construct a varray.
  month MONTHS_VARRAY := 
    months_varray('January','February','March'
                 ,'April','May','June'
                 ,'July','August','September'
                 ,'October','November','December');

  -- Declare an associative array variable.
  calendar CALENDAR_TABLE;
BEGIN
  -- Check if calendar has no elements.
  IF calendar.COUNT = 0 THEN
    -- Print a title

    DBMS_OUTPUT.PUT_LINE('Assignment loop:');
    DBMS_OUTPUT.PUT_LINE('----------------');
    -- Loop through all the varray elements.

    FOR i IN month.FIRST..month.LAST LOOP
      -- Initialize a null associative array element.
      calendar(i) := '';
      -- Print an indexed element from the associative array.
      DBMS_OUTPUT.PUT_LINE(
        'Index ['||i||'] is ['||calendar(i)||']');
      -- Assign the numeric index valued varray element
      -- to an equal index valued associative array element.
      calendar(i) := month(i);
    END LOOP;
    -- Print a title
    DBMS_OUTPUT.PUT(CHR(10));
    DBMS_OUTPUT.PUT_LINE('Post-assignment loop:');
    DBMS_OUTPUT.PUT_LINE('---------------------');
    -- Loop through all the associative array elements.
    FOR i IN calendar.FIRST..calendar.LAST LOOP
      -- Print an indexed element from the associative array.
      DBMS_OUTPUT.PUT_LINE(
        'Index ['||i||'] is ['||calendar(i)||']');
    END LOOP;
  END IF;
END;
我的异常网推荐解决方案:oracle存储过程,http://www.aiyiweb.com/oracle-develop/177537.html