pl/sql集合型別(一)

wisdomone1發表於2011-09-23
/------------------------------------------------------------------------------------------------------------------------------------/
/--------------------------------------------------------     pl/sql集合     --------------------------------------------------------/
/------------------------------------------------------------------------------------------------------------------------------------/
                      
                        
                        
pl/sql集合
  它是為了處理多行單列資料
 
  pl/sql集合型別包括:
                       索引表 pl/sql表
                       巢狀表nested table
                       變長陣列varray
                      
                      
                       索引表
                            也叫pl/sql表,它是oracle是期版本處理PL/SQL陣列的資料型別;它的元素個數無限制,並且下標可以為負值
                           
                            定義PL/SQL索引表的語法:
                                                        type type_name is table of element_type [not null] index by key_type;
                                                        identifier type_name;
                                                       
                                                        補充:
                                                             not null表示不允許引用NULL無素,
                                                             key_type用於指定索引表元素下標的資料型別(binary_integer,pls_integer或varchar2)
                                                            
                                                        在oracle9i前,索引表下標只充許使用資料型別binary_integer和pls_integer;
                                                         自ORACLE9I開始,索引表下標也允許為VARCHAR2
                                                        
                                                        
                                                        
                          示例一:在索引表中使用BINARY_INTEGER和PLS_INTEGER
                                                   SET SERVEROUTPUT ON
                                                       DELCARE
                                                         TYPE ENAME_TABLE_TYPE IS TABLE OF EMP.ENAME%TYPE INDEX BY BINARY_INTEGER;
                                                         ENAME_TABLE ENAME_TABLE_TYPE;
                                                       BEGIN
                                                         SELECT ENAME INTO ENAME_TABLE(_1) FROM EMP WHERE EMPNO=&NO;
                                                         DBMS_OUTPUT.PUT_LINE('僱員名:'||ENAME_TABLE(-1));--表示下標為-1的元素
                                                       END;
                                                       / 
                                                       END;    
                          
                          示例二:在索引表中使用VARCHAR2下標資料型別
                                                       set serveroutput on
                                                       declare
                                                          type area_table_type is table of number index by varchar2(10);
                                                          area_table area_table_type;
                                                       begin
                                                         area_table('beijing'):=1;
                                                         area_table('shanghai'):=2;
                                                         dbms_output.put_line('first element:'||area_table.first);--顯示第一個元素,注意FIRST
                                                         DBMS_OUTPUT.PUT_LINE('LAST ELEMENT'||AREA_TABLE.LAST);
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
            巢狀表
                                      是一種用於處理PL/SQL陣列的資料型別;它的元素下標從1開始,並且元素個數沒有限制;
                                       它的元素的元素值可以是稀疏,即元素之間可有空位出現
                                     
                                      索引表型別不能作用表列的資料型別使用,但巢狀表型別可以作為表列的資料型別使用:
                                     
                                      定義巢狀表的語法如下:
                                         type type_name is table of element_type;
                                         identifier type_name;
                                        
                                      注意:在使用巢狀表元素時,必須首先使用其構造方法初始化巢狀表;示例如下;
                                         declare
                                           type ename_table_type is table of emp.ename%type;
                                           ename_table ename_table_type:=ename_table_type('a','b');
                                          
                                          
                                          
                                      下面說明使用巢狀表的方法:
                                         1,在PL/SQL塊中使用巢狀表
                                            declare
                                                   type ename_table_type is table of emp.ename%type;
                                                   ename_table ename_table_type;
                                               begin
                                                    --使用前先初始化
                                                   ename_table:=ename_table_type('a','b','c');--NESTED TABLE初始化已有資料                                                        
                                                   select ename into ename_table(2) from emp where empno=&no;--利用SELECT INTO為NESTED TABLE元素供值,作用與UPDATE相同
                                                  
                                                   dbms_output.put_line('employee name:'||ename_table(2));
                                               end;
                                               /
                                                 
                                                 
                                                 
                                              SQL>
                                              --下面為直接初始使用,未使用SELECT INTO
                                        SQL> declare
                                          2     type ename_table_type is table of emp.ename%type;
                                          3     ename_table ename_table_type;
                                          4  begin
                                          5      --使用前先初始化
                                          6     ename_table:=ename_table_type('a','b','c');
                                          7     --select ename into ename_table(2) from emp where empno=&no;
                                          8     dbms_output.put_line('employee name:'||ename_table(2));
                                          9  end;
                                         10  /
                                       
                                        employee name:b
                                       
                                       
                                       
                                        2,在表列中使用NESTED TABLE
                                          
                                             在表列中使用NESTED TABLE,必須首先使用CREATE TYPE建立NESTED TABLE型別
                                              
                                             在表列中使用NESTED TABLE時,必須要為對應的表列指定專門的儲存表
                                          
                                               在表列中使用NESTED TABLE型別的示例如下:
                                                  create type phone_type is table of varchar2(20);
                                                  /
                                                  create table employee(
                                                   id number(4),
                                                   name varchar2(10,
                                                   sal number(6,2),
                                                   phone phone_type --表列PHONE使用型別PHONE_TYPE
                                                   ) NESTED TABLE PHONE STORE AS PHONE_TABLE;--要為NESTED TABLE表列指定專門的儲存表
                                                  
                                                  
                                        下列說明在PL/SQL塊中操作NESTED TABLE表列的方法:
                                          
                                           示例一:在PL/SQL塊中為NESTED TABLE表列插入資料
                                                   begin
                                                      insert into employee values(1,'scott',800,phone_type('0472','12255444'));
                                                   end;
                                                    /
                                                   
                                              示例二:在PL/SQL塊中檢索NESTED TABLE 表列的資料
                                                 set serveroutput on
                                                 declare
                                                   phone_table phone_type;--宣告接受表列為NESTED TABLE列的表列
                                                 begin
                                                    select phone into phone_table  把表EMPLOYEE的NESTED TABLE表列 PHONE儲存到變數PHONE_TABLE
                                                    from employee where id=1;
                                                   
                                                     --用FOR  LOOP顯示NESTED TABLE型別的變數PHONE_TABLE中的值
                                                    FOR I IN 1..PHONE_TABLE.COUNT LOOP --count表示NESTED TABLE變數總共的元素個數
                                                      DBMS_OUTPUT.PUT_LINE('PHONE NUMBER:'||PHONE_TABLE(I));--phone_table(i) 表示NESTED TABLE具體的元素
                                                    END LOOP;
                                                   
                                                 end;
                                                     
                                                     
                                              示例三:在PL/SQL塊中更新NESTED TABLE    表列的資料
                                                  declare
                                                    phone_table phone_type:=phone_type('0412','abc','toxls','lvs');   
                                                  begin
                                                    update employee
                                                    set phone=phone_table where id=1;
                                                  end;
                                           

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-708196/,如需轉載,請註明出處,否則將追究法律責任。

相關文章