Oracle数组的使用

下面这组信息比较有用,列出来以备参考:

除了构造函数外,集合还有很多内建函数,这些函数称为方法。

调用方法的语法如下:

collection.method

下表中列出oracle中集合的方法

方法 描述 使用限制

COUNT 返回集合中元素的个数

DELETE 删除集合中所有元素

DELETE() 删除元素下标为x的元素,如果x为null,则集合保持不变          对VARRAY非法

DELETE(,) 删除元素下标从X到Y的元素,如果X>Y集合保持不变             对VARRAY非法

EXIST() 如果集合元素x已经初始化,则返回TRUE, 否则返回FALSE

EXTEND 在集合末尾添加一个元素                                       对Index_by非法

EXTEND() 在集合末尾添加x个元素                                      对Index_by非法

EXTEND(,) 在集合末尾添加元素n的x个副本                              对Index_by非法

FIRST 返回集合中的第一个元素的下标号,对于VARRAY集合始终返回1。

LAST 返回集合中最后一个元素的下标号, 对于VARRAY返回值始终等于COUNT.

LIMIT 返回VARRY集合的最大的元素个数,对于嵌套表和对于嵌套表和Index_by为null Index_by集合无用

NEXT() 返回在元素x之后及紧挨着它的元素的值,如果该元素是最后一个元素,则返回null.

PRIOR() 返回集合中在元素x之前紧挨着它的元素的值,如果该元素是第一个元素,则返回null。

TRI M 从集合末端开始删除一个元素                                    对于index_by不合法

TRIM() 从集合末端开始删除x个元素                                    对index_by不合法

使用实例:

Set serveroutput on

Declare

    type my_text_table_type is table of varchar2(200)

     index by binary_integer;

    l_text_table my_text_table_type;

    l_index number;

begin

for emp_rec in (select * from emp) loop

   l_text_table(emp_rec.empno):=emp_rec.ename;

end loop;

   l_index:= l_text_table.first;--使用first方法

       loop

              exit when l_index is null;

              dbms_output.put_line(l_index ||’:’|| l_text_table(l_index));

              l_index :=l_text_table.next(l_index);

       end loop;

end;

实例:

CREATE OR REPLACE PROCEDURE sample  

is  

    TYPE R_REC IS RECORD(INT     NUMBER(6,2), CHR    VARCHAR2(100));

    TYPE T_REC IS TABLE OF R_REC INDEX BY BINARY_INTEGER;

    A_ZEI T_REC;    

    IX      NUMBER(10);    

BEGIN  

    FOR IX IN 1..1000 LOOP  

        A_ZEI(IX).INT := IX;

        A_ZEI(IX).CHR := TO_CHAR(A_ZEI(IX).INT,'9,999,999.99');  

    END LOOP;  

END;  

CREATE OR REPLACE PROCEDURE P_EMP

IS

     TYPE T_EMP IS TABLE OF EMP%ROWTYPE INDEX BY BINARY_INTEGER;

     A_EMP T_EMP;

     I        BINARY_INTEGER := 0;

BEGIN

     FOR REC IN (SELECT EMPNO,ENAME FROM EMP) LOOP

        I := I + 1;

        A_EMP(I).EMPNO := REC.EMPNO;

        A_EMP(I).ENAME := REC.ENAME;

     END LOOP;

     FOR K IN 1..I LOOP

        DBMS_OUTPUT.PUT_LINE( A_EMP(K).EMPNO || '       ' || A_EMP(K).ENAME);

     END LOOP;

END;

    集合:是具有相同定义的元素的聚合。Oracle有两种类型的集合:

可变长数组(VARRAY):可以有任意数量的元素,但必须预先定义限制值。

嵌套表:视为表中之表,可以有任意数量的元素,不需要预先定义限制值。

在PL/SQL中是没有数组(Array)概念的。但是如果程序员想用Array的话,就得变通一下,用TYPE 和Table of Record来代替多维数组,一样挺好用的。

emp_type 就好象一个table 中的一条record 一样,里面有id, name,gender等。emp_type_array 象个table, 里面含有一条条这样的record (emp_type),就象多维数组一样。

--单维数组

DECLARE

TYPE emp_ssn_array IS TABLE OF NUMBER

INDEX BY BINARY_INTEGER;

best_employees emp_ssn_array;

worst_employees emp_ssn_array;

BEGIN

best_employees(1) := '123456';

best_employees(2) := '888888';

worst_employees(1) := '222222';

worst_employees(2) := '666666';

FOR i IN 1..best_employees.count LOOP

DBMS_OUTPUT.PUT_LINE('i='|| i || ', best_employees= ' ||best_employees(i)

|| ', worst_employees= ' ||worst_employees(i));

END LOOP;

END;

--多维数组

DECLARE

TYPE emp_type IS RECORD

( emp_id employee_table.emp_id%TYPE,

emp_name employee_table.emp_name%TYPE,

emp_gender employee_table.emp_gender%TYPE );

TYPE emp_type_array IS TABLE OF emp_type INDEX BY BINARY_INTEGER;

emp_rec_array emp_type_array;

emp_rec emp_type;

BEGIN

emp_rec.emp_id := 300000000;

emp_rec.emp_name := 'Barbara';

emp_rec.emp_gender := 'Female';

emp_rec_array(1) := emp_rec;

emp_rec.emp_id := 300000008;

emp_rec.emp_name := 'Rick';

emp_rec.emp_gender := 'Male';

emp_rec_array(2) := emp_rec;

FOR i IN 1..emp_rec_array.count LOOP

DBMS_OUTPUT.PUT_LINE('i='||i

||', emp_id ='||emp_rec_array(i).emp_id

||', emp_name ='||emp_rec_array(i).emp_name

||', emp_gender = '||emp_rec_array(i).emp_gender);

END LOOP;

END;

-------------- Result --------------

i=1, emp_id =300000000, emp_name =Barbara, emp_gender = Female

i=2, emp_id =300000008, emp_name =Rick, emp_gender = Male

用下面语句声明数组类型

type intarray is varry(30) of integer;

用下面语句声明一个数组变量

declare

A intarray;用下面语句声明数组类型

type intarray is varry(30) of integer;

用下面语句声明一个数组变量

declare

A intarray;用下面语句声明数组类型

type intarray is varry(30) of integer;

用下面语句声明一个数组变量

declare

A intarray;


猜你喜欢

转载自baobaojinjin.iteye.com/blog/1579027