全部博文(136)
分类: Oracle
2008-05-15 11:45:29
Method (function or procedure) |
Description |
---|---|
COUNT function |
Returns the current number of elements in a collection. |
DELETE procedure |
Removes one or more elements from the collection. Reduces COUNT if the element is not already DELETEd. With VARRAYS, you can delete only the entire contents of the collection. |
EXISTS function |
Returns TRUE or FALSE to indicate whether the specified element exists. |
EXTEND procedure |
Increases the number of elements in a nested table or VARRAY. Increases COUNT. |
FIRST, LAST functions |
Returns the smallest (FIRST) and largest (LAST) subscripts in use. |
|
|
LIMIT function |
Returns the maximum number of elements allowed in a VARRAY. |
PRIOR, NEXT functions |
Returns the subscript immediately before (PRIOR) or after (NEXT) a specified subscript. You should always use PRIOR and NEXT to traverse a collection, especially if you are working with sparse (or potentially sparse) collections. |
|
|
TRIM procedure |
Removes collection elements from the end of the collection (highest defined subscript). Reduces COUNT if elements are not DELETEd. |
IF LAST (company_table) > 10 THEN ... /* 错误的语法 */ |
IF company_table.LAST > 10 THEN ... /* 正确的语法*/ |
FUNCTION COUNT RETURN PLS_INTEGER; |
SQL> set serveroutput on SQL> declare 2 t_colors type_colors := type_colors('red'); 3 begin 4 if t_colors.count > 0 then 5 dbms_output.put_line( t_colors(1) ); 6 end if; 7 end; 8 / red PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 t_colors type_colors := type_colors( ); 3 begin 4 dbms_output.put_line( 'COUNT: ' || t_colors.count ); 5 end; SQL> / COUNT: 0 PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 t_colors type_colors ; 3 begin 4 dbms_output.put_line( 'COUNT: ' || t_colors.count ); 5* end; SQL> / declare * ERROR 位于第 1 行: ORA-06531: 引用未初始化的收集 ORA-06512: 在line 4 SQL> declare 2 t_colors type_colors ; 3 begin 4 dbms_output.put_line( 'COUNT: ' || t_colors.count ); 5 exception 6 when collection_is_null then 7 dbms_output.put_line( 'The Collection is null.' ); 8 end; SQL> / The Collection is null. PL/SQL 过程已成功完成。 SQL> |
PROCEDURE DELETE; PROCEDURE DELETE (i [BINARY_INTEGER | VARCHAR2(size_limit)]); PROCEDURE DELETE (i [BINARY_INTEGER | VARCHAR2(size_limit)], j [BINARY_INTEGER | VARCHAR2(size_limit)]); |
SQL> declare 2 type type_names is table of varchar2(20); 3 t_names type_names := type_names(); 4 begin 5 t_names.extend(2); 6 t_names(1) := 'yuechaotian'; 7 t_names(2) := 'yuexingtian'; 8 t_names.delete(2); 9 dbms_output.put_line( 'After delete, the COUNT is: ' || t_names.count ); 10 t_names(2) := 'yuexingtian'; 11 dbms_output.put_line( 'After re-insert, the COUNT is: ' || t_names.count ); 12 end; SQL> / After delete, the COUNT is: 1 After re-insert, the COUNT is: 2 PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 type type_names is table of varchar2(20); 3 t_names type_names := type_names(); 4 begin 5 t_names.extend(2); 6 t_names(1) := 'yuechaotian'; 7 t_names(2) := 'yuexingtian'; 8 t_names.delete(-5); 9 dbms_output.put_line( 'After delete, the COUNT is: ' || t_names.count ); 10 t_names.delete(-3, 1); 11 dbms_output.put_line( 'After re-insert, the COUNT is: ' || t_names.count ); 12 end; SQL> / After delete, the COUNT is: 2 After re-insert, the COUNT is: 1 PL/SQL 过程已成功完成。 SQL> |
FUNCTION EXISTS (i IN [BINARY_INTEGER | VARCHAR2(size_limit)]) RETURN BOOLEAN; |
SQL> declare 2 type type_names is table of varchar2(20); 3 t_names type_names := type_names(); 4 begin 5 t_names.extend(2); 6 t_names(1) := 'yuechaotian'; 7 if t_names.exists(2) then 8 dbms_output.put_line( 'There is an element in row 2.' ); 9 else 10 dbms_output.put_line( 'There is no element in row 2.' ); 11 end if; 12 t_names.delete(2); 13 if t_names.exists(2) then 14 dbms_output.put_line( 'There is a element in row 2.' ); 15 else 16 dbms_output.put_line( 'There is no element in row 2.' ); 17 end if; 18 end; 19 / There is an element in row 2. There is no element in row 2. PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 type type_names is table of varchar2(20); 3 t_names type_names := type_names(); 4 begin 5 if t_names.exists(1) then 6 dbms_output.put_line('COUNT: ' || t_names.count); 7 else 8 dbms_output.put_line( 'The nested table has no element or has not been initialized.' ); 9 end if; 10 end; SQL> / The nested table has no element or has not been initialized. PL/SQL 过程已成功完成。 SQL> |
PROCEDURE EXTEND (n PLS_INTEGER:=1); PROCEDURE EXTEND (n PLS_INTEGER, i PLS_INTEGER); |
SQL> declare 2 type type_names is table of varchar2(20) not null; 3 t_names type_names := type_names(); 4 begin 5 t_names.extend; 6 t_names(1) := 'yuechaotian'; 7 t_names.extend(4, 1); 8 for n_pointer in t_names.first..t_names.last loop 9 dbms_output.put_line( 'element ' || n_pointer || ' : ' || t_names(n_pointer) ); 10 end loop; 11 end; SQL> / element 1 : yuechaotian element 2 : yuechaotian element 3 : yuechaotian element 4 : yuechaotian element 5 : yuechaotian PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 type type_names is table of varchar2(20) not null; 3 t_names type_names := type_names(); 4 begin 5 t_names.extend; 6 t_names(1) := 'yuechaotian'; 7 t_names.extend(4, 1); 8 t_names.delete(5); 9 t_names.extend(2, 1); 10 for n_pointer in t_names.first..t_names.last loop 11 if t_names.exists(n_pointer) then 12 dbms_output.put_line( 'element ' || n_pointer || ' : ' || t_names(n_pointer) ); 13 else 14 dbms_output.put_line( 'element ' || n_pointer || ' is not exists. ' ); 15 end if; 16 end loop; 17* end; SQL> / element 1 : yuechaotian element 2 : yuechaotian element 3 : yuechaotian element 4 : yuechaotian element 5 is not exists. element 6 : yuechaotian element 7 : yuechaotian PL/SQL 过程已成功完成。 SQL> |
FUNCTION FIRST RETURN PLS_INTEGER; FUNCTION LAST RETURN PLS_INTEGER; |
1. 创建环境
SQL> create table test1 ( a varchar2(20) ); 表已创建。 SQL> insert into test1 select 'yuechaotian' from dba_objects where rownum <6; 已创建5行。 SQL> commit; 提交完成。 SQL> select * from test1; A -------------------- yuechaotian yuechaotian yuechaotian yuechaotian yuechaotian SQL> |
SQL> declare 2 type type_names is table of test1.a%type index by pls_integer; 3 t_names type_names; 4 begin 5 for rec_names in( select * from test1 ) loop 6 t_names( nvl(t_names.last, 0) + 1 ) := rec_names.a; 7 end loop; 8 for n_pointer in t_names.first..t_names.last loop 9 dbms_output.put_line( n_pointer || ' : ' || t_names(n_pointer) ); 10 end loop; 11 end; 12 / 1 : yuechaotian 2 : yuechaotian 3 : yuechaotian 4 : yuechaotian 5 : yuechaotian PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 type type_names is table of test1.a%type index by pls_integer; 3 t_names type_names; 4 begin 5 for rec_names in( select * from test1 ) loop 6 t_names( t_names.count + 1 ) := rec_names.a; 7 end loop; 8 for n_pointer in t_names.first..t_names.last loop 9 dbms_output.put_line( n_pointer || ' : ' || t_names(n_pointer) ); 10 end loop; 11 end; 12 / 1 : yuechaotian 2 : yuechaotian 3 : yuechaotian 4 : yuechaotian 5 : yuechaotian PL/SQL 过程已成功完成。 SQL> |
FUNCTION LIMIT RETURN PLS_INTEGER; |
IF my_list.LAST < my_list.LIMIT THEN my_list.EXTEND; END IF; |
FUNCTION PRIOR (i [BINARY_INTEGER | VARCHAR2(size_limit)]) RETURN [BINARY_INTEGER | VARCHAR2(size_limit)]; FUNCTION NEXT (i [BINARY_INTEGER | VARCHAR2(size_limit)]) RETURN [BINARY_INTEGER | VARCHAR2(size_limit)]; |
SQL> select * from test1; A B
SQL> declare 2 type type_score is table of test1.b%type index by pls_integer; 3 t_score type_score; 4 n_total number(10) := 0; 5 n_pointer pls_integer; 6 begin 7 for rec_test1 in ( select * from test1 ) loop 8 t_score(rec_test1.b) := rec_test1.b; 9 end loop; 10 n_pointer := t_score.first; 11 while n_pointer is not null loop 12 n_total := n_total + t_score(n_pointer); 13 n_pointer := t_score.next(n_pointer); 14 end loop; 15 dbms_output.put_line( 'Total: ' || n_total ); 16 end; SQL> / Total: 4745 PL/SQL 过程已成功完成。 SQL> |
10 n_pointer := t_score.last; 11 while n_pointer is not null loop 12 n_total := n_total + t_score(n_pointer); 13 n_pointer := t_score.prior(n_pointer); 14 end loop; |
SQL> declare 2 type type_names is table of varchar2(20) index by pls_integer; 3 t_names type_names; 4 begin 5 t_names(-53) := 'yuechaotian'; 6 t_names(0) := 'yuexingtian'; 7 t_names(43) := 'tianyc'; 8 dbms_output.put_line( 'PRIOR 100: ' || t_names.prior(100) ); 9 dbms_output.put_line( 'NEXT -100: ' || t_names.next(-100) ); 10* end; SQL> / PRIOR 100: 43 NEXT -100: -53 PL/SQL 过程已成功完成。 SQL> |
PROCEDURE TRIM (n PLS_INTEGER:=1); |
SQL> declare
PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 type type_names is table of varchar2(20); 3 t_names type_names := type_names(); 4 begin 5 t_names.extend(3); 6 t_names(1) := 'yuechaotian'; 7 t_names(2) := 'yuexingtian'; 8 t_names(3) := 'tianyc'; 9 t_names.delete(2); 10 t_names.trim(2); 11 for n_pointer in 1..3 loop 12 if t_names.exists( n_pointer ) then 13 dbms_output.put_line( 'element ' || n_pointer || ': ' || t_names(n_pointer) ); 14 else 15 dbms_output.put_line( 'element ' || n_pointer || ' is not exists. ' ); 16 end if; 17 end loop; 18* end; SQL> / element 1: yuechaotian element 2 is not exists. element 3 is not exists. PL/SQL 过程已成功完成。 SQL> |