在oracle存储过程中如何创建数组?

在oracle存储过程中,可以创建类似于数组的东西,我记得好像是使用type语句,具体记不清楚了,不知道那位可以告诉我,谢谢!
---------------------------------------------------------------

type index_table is table of varchar2(2000) index by binary_integer;
type nested_table is table of varchar2(2000);
type v_arrary is varray(1000) of varchar2(2000);
---------------------------------------------------------------

A PL/SQL table is a single column array. The PL/SQL table is indexed by a binary integer counter so you can reference any value stored in a PL/SQL table in an indexed fashion. The number of rows in a PL/SQL table is dynamic, it can be increased as desired.

PL/SQL tables are declared in two steps. First, the table type is declared, then the PL/SQL table variable is assigned to that table type.

The PL/SQL declarations can occur in any block of a pl/sql construct. A PL/SQL table cannot be initialized in its declaration. The first reference to a PL/SQL table must be an assignment statement or a NO_DATA_FOUND will result. PL/SQL tables follow the same scope rules as other variables and cease to exist when the package, function or procedure is exited.

Example:

In this example, we declare a table type of NUMBER and then assign two tables, results and n10. Notice how the indexes are used in each to specify a specific value.

CREATE OR REPLACE FUNCTION max_value RETURN NUMBER IS
TYPE numtab IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;
results numtab;
n10 numtab;
max_loop NUMBER;
temp_store NUMBER;
CURSOR get_results IS
SELECT result FROM result_table;
i integer;
x integer:=1;
j integer:=1;
BEGIN
OPEN get_results;
FOR i IN 1..1500 LOOP
FETCH get_results INTO temp_store;
IF i = MOD(x,15) THEN
results(j):=temp_store;
END IF;
END LOOP;
j:=1;
FOR i IN 1..10 LOOP
n10(i):=GREATEST(results(j),results(j+1),results(j+2),
results(j+3),results(j+4),results(j+6),
results(j+7),results(j+7),results(j+8),
results(j+9));
j:=j+10;
END LOOP;
j:=1
max_loop:=GREATEST(n10(j),n10(j+1),n10(j+2),
n10(j+3),n10(j+4),n10(j+5),n10(j+6),n10(j+7),
n10(j+8),n10(j+9);
RETURN max_loop;
END;

Published At
Categories with 数据库类
comments powered by Disqus