Sie sind auf Seite 1von 6

Collection

A collection groups elements of the same type in a specified order. Each element has a unique subscript that determines its position in the collection. PL/SQL has three kinds of collections: Associative arrays (formerly called "PL/SQL tables" or "index-by tables") Nested tables Variable-size arrays (varrays) Associative arrays can be indexed by either integers or strings. Nested tables and varrays are indexed by integers. To create a collection, you first define a collection type, and then declare a variable of that type. Note: This topic applies to collection types that you define inside a PL/SQL block or package, which are different from standalone stored collection types that you create with the CREATE TYPE Statement. In a PL/SQL block or package, you can define all three collection types. With the CREATE TYPE statement, you can create nested table types and varray types, but not associative array types. Syntax collection_type_definition ::=

Description of the illustration collection_type_def.gif assoc_array_type_def ::=

Description of the illustration assoc_array_type_def.gif (element_type ::=) nested_table_type_def ::=

Description of the illustration nested_table_type_def.gif (element_type ::=) varray_type_def ::=

Description of the illustration varray_type_def.gif (element_type ::=) collection_variable_dec ::= Description of the illustration collection_variable_dec.gif element_type ::=

Description of the illustration element_type_definition.gif Keyword and Parameter Descriptions collection_name The name that you give to the variable of the collection type that you defined. element_type The data type of the collection element (any PL/SQL data type except REF CURSOR). For a nested table: If element_type is an object type, then the nested table type describes a table whose columns match the name and attributes of the object type. If element_type is a scalar type, then the nested table type describes a table with a single, scalar type column called COLUMN_VALUE. You cannot specify NCLOB for element_type. However, you can specify CLOB or BLOB. INDEX BY For an associative array, the data type of its indexesPLS_INTEGER, BINARY_INTGER, or VARCHAR2. NOT NULL Specifies that no element of the collection can have the value NULL. size_limit For a varray, a positive integer literal that specifies the maximum number of elements it can contain.

A maximum limit is imposed. See Referencing Collection Elements. type_name The name that you give to the collection type that you are defining. v_size For an associative array, the length of the VARCHAR2 key by which it is indexed. Usage Notes The type definition of an associative array can appear only in the declarative part of a block, subprogram, package specification, or package body. The type definition of a nested table or varray can appear either in the declarative part of a block, subprogram, package specification, or package body (in which case it is local to the block, subprogram, or package) or in the CREATE TYPE Statement (in which case it is a standalone stored type). Nested tables extend the functionality of associative arrays, so they differ in several ways. See Choosing Between Nested Tables and Associative Arrays. Nested tables and varrays can store instances of an object type and, conversely, can be attributes of an object type. Collections work like the arrays of most third-generation programming languages. A collection has only one dimension. To model a multidimensional array, declare a collection whose items are other collections. Collections can be passed as parameters. You can use them to move columns of data into and out of database tables or between client-side applications and stored subprograms. Every element reference includes the collection name and one or more subscripts enclosed in parentheses; the subscripts determine which element is processed. Except for associative arrays, which can have negative subscripts, collection subscripts have a fixed lower bound of 1. Subscripts for multilevel collections are evaluated in any order; if a subscript includes an expression that modifies the value of a different subscript, the result is undefined. See Referencing Collection Elements. Associative arrays and nested tables can be sparse (have nonconsecutive subscripts), but varrays are always dense (have consecutive subscripts). Unlike nested tables, varrays retain their ordering and subscripts when stored in the database. Initially, associative arrays are sparse. That enables you, for example, to store reference data in a temporary variable using a primary key (account numbers or employee numbers for example) as the index. Collections follow the usual scoping and instantiation rules. In a package, collections are instantiated when you first reference the package and cease to exist when you end the database session. In a block or subprogram, local collections are instantiated when you enter the block or subprogram and cease to exist when you exit. Until you initialize it, a nested table or varray is atomically null (that is, the collection itself is null, not its elements). To initialize a nested table or varray, you use a constructor, which is a systemdefined function with the same name as the collection type. This function constructs (creates) a collection from the elements passed to it. For information about collection comparisons that are allowed, see Comparing Collections. Collections can store instances of an object type and, conversely, can be attributes of an object type. Collections can also be passed as parameters. You can use them to move columns of data into and out of database tables or between client-side applications and stored subprograms.

When invoking a function that returns a collection, you use the following syntax to reference elements in the collection:
function_name(parameter_list)(subscript)

See Example 5-16, "Referencing an Element of an Associative Array" and Example B-2, "Using the Dot Notation to Qualify Names". With the Oracle Call Interface (OCI) or the Oracle Precompilers, you can bind host arrays to associative arrays (index-by tables) declared as the formal parameters of a subprogram. That lets you pass host arrays to stored functions and procedures. Collection Methods A variety of methods exist for collections, but not all are relevant for every collection type: EXISTS(n) - Returns TRUE if the specified element exists. COUNT - Returns the number of elements in the collection. LIMIT - Returns the maximum number of elements for a VARRAY, or NULL for nested tables. FIRST - Returns the index of the first element in the collection. LAST - Returns the index of the last element in the collection. PRIOR(n) - Returns the index of the element prior to the specified element. NEXT(n) - Returns the index of the next element after the specified element. EXTEND - Appends a single NULL element to the collection. EXTEND(n) - Appends n NULL elements to the collection. EXTEND(n1,n2) - Appends n1 copies of the n2th element to the collection. TRIM - Removes a single element from the end of the collection. TRIM(n) - Removes n elements from the end of the collection. DELETE - Removes all elements from the collection. DELETE(n) - Removes element n from the collection. DELETE(n1,n2) - Removes all elements from n1 to n2 from the collection. Triggers and PL/SQL select * from user_errors where name='GET_EMP_RS'; select * from dept; select * from emp; select * from student; CREATE OR REPLACE PROCEDURE get_emp_rs (p_deptno IN emp.deptno%TYPE, p_recordset OUT SYS_REFCURSOR) AS BEGIN OPEN p_recordset FOR SELECT ename, empno, deptno FROM emp WHERE deptno = p_deptno

ORDER BY ename; END Get_Emp_RS; exec Get_Emp_RS(10,emp); declare cursor c is select ename,cursor(select dname from dept d where e.empno=d.deptno) from emp e; type t is ref cursor; c1 t; c2 t; v1 emp.ename%type; v2 emp.dname%type; begin open c; loop fetch c1 into v1; exit when c1%notfound; fetch c2 into v2; exit when c2%notfound; dbms_output.put_line('ename='||v1||'dname='||v2); end loop; end loop; close c; end; / DECLARE type t is varray(4) of emp%rowtype; va t := t(null,null,null,null); BEGIN for i in 1..va.count loop select * into va(i) from emp where empno = i; dbms_output.put_line('empno = ' || va(i).empno || ' ename = ' || va(i).ename); end loop; exception when no_data_found then null; END; create or replace procedure ref_cursor(table_name in varchar) is type t is REF CURSOR; c t; v_dept dept%rowtype; type r is record (ename emp.ename%type,job emp.job%type,sal emp.sal%type); v_emp r; begin if table_name='dept' then open c for select * from dept; elsif table_name='emp' then open c for select ename,job,sal from emp; end if; loop

if table_name='dept' then fetch c into v_dept; exit when c%notfound; dbms_output.put_line('deptno='||v_dept.deptno||'dname='||v_dept.dname||'loc='||v_dept.loc); elsif table_name='emp' then fetch c into v_emp; exit when c%notfound; dbms_output.put_line('ename='||v_emp.ename||'job='||v_emp.job||'sal='||v_emp.sal); end if; end loop; close c; end; exec ref_cursor('dept'); exec ref_cursor('emp');

Das könnte Ihnen auch gefallen