PL SQL Collection

November 15, 2017 | Author: writesmd | Category: Pl/Sql, Databases, Data, Areas Of Computer Science, Computer Data
Share Embed Donate


Short Description

Oracle PLSQL Collections...

Description

PL/SQL RECORD A record is a group of related data items stored in fields, each with its own name and datatype. Suppose you have various data about an employee such as name, salary, and hire date. These items are logically related but dissimilar in type. A record containing a field for each item lets you treat the data as a logical unit. Thus, records make it easier to organize and represent information. The attribute %ROWTYPE lets you declare a record that represents a row in a database table. However, you cannot specify the datatypes of fields in the record or declare fields of your own. The datatype RECORD lifts those restrictions and lets you define your own records. Manipulating Records The datatype RECORD letxs you collect information about the attributes of something. The information is easy to manipulate because you can refer to the collection as a whole. In the following example, you collect accounting figures from database tables assets and liabilities, then use ratio analysis to compare the performance of two subsidiary companies: DECLARE TYPE FiguresRec IS RECORD (cash REAL, notes REAL, ...); Sub1_figs FiguresRec; Sub2_figs FiguresRec; BEGIN

FUNCTION acid_test (figs FiguresRec) RETURN REAL IS... SELECT cash, notes ... INTO sub1_figs FROM assets, liabilities WHERE assets.sub = 1 AND liabilities.sub = 1; SELECT cash, notes ... INTO sub2_figs FROM assets, liabilities WHERE assets.sub = 2 AND liabilities.sub = 2; IF acid_test (sub1_figs) > acid_test (sub2_figs) THEN... ...

END; Notice how easy it is to pass the collected figures to the function acid_test, which

computes a financial ratio.

----------------------------------------------------------------------------------------------------------------------------In the example below, you fetch rows from database table flights into record flight_info. That way, you can treat all the information about a flight, including its passenger list, as a logical unit. DECLARE TYPE FLIGHTREC IS RECORD ( FLIGHT_NO NUMBER(3), GATE CHAR(5), DEPARTURE CHAR(15), ARRIVAL CHAR(15), PASSENGERS PASSENGERLIST); FLIGHT_INFO FLIGHTREC; CURSOR C1 IS SELECT * FROM FLIGHTS; SEAT_NOT_AVAILABLE EXCEPTION;

PL/SQL Collections

1

BEGIN

OPEN C1; LOOP FETCH C1 INTO FLIGHT_INFO; EXIT WHEN C1%NOTFOUND; FOR I IN 1...FLIGHT_INFO.PASSENGERS.LAST LOOP IF FLIGHT_INFO.PASSENGERS (I).SEAT = ’NA’ THEN DBMS_OUTPUT.PUT_LINE (FLIGHT_INFO.PASSENGERS (I).NAME); RAISE SEAT_NOT_AVAILABLE; END IF; ... END LOOP; END LOOP; CLOSE C1; EXCEPTION WHEN SEAT_NOT_AVAILABLE THEN ... END; PL/SQL TABLE In the example below, with each iteration of the loop, the FETCH statement fetches ten rows (or less) into index-by table empnos. The previous values are overwritten. DECLARE TYPE NUMTAB IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; CURSOR C1 IS SELECT EMPNO FROM EMP; EMPNOS NUMTAB; ROWS NATURAL := 10; BEGIN OPEN C1; LOOP /* THE FOLLOWING STATEMENT FETCHES 10 ROWS (OR LESS). */ FETCH C1 BULK COLLECT INTO EMPNOS LIMIT ROWS; EXIT WHEN C1%NOTFOUND; ... END LOOP; CLOSE c1; END;

PL/SQL Collections

2

BULK COLLECT / FORALL / DYNAMIC SQL CREATE OR REPLACE PROCEDURE bulkcollect_9i_demo (whr_in IN VARCHAR2 := NULL) IS TYPE numlist_t IS TABLE OF NUMBER; TYPE namelist_t IS TABLE OF VARCHAR2 (100); -- New Oracle9i pre-defined REF CURSOR type. This is equivalent to: -TYPE sys_refcursor IS REF CURSOR emp_cv sys_refcursor; empnos numlist_t; enames namelist_t; enames_updated namelist_t; ename_filter namelist_t := namelist_t ('S%', 'E%', 'M%'); sals numlist_t; l_count PLS_INTEGER; bulk_errors EXCEPTION; PRAGMA EXCEPTION_INIT ( bulk_errors, -24381 ); BEGIN -- Bulk fetch with cursor variable OPEN emp_cv FOR 'SELECT empno, ename FROM emp WHERE ' || NVL (whr_in, '1=1'); FETCH emp_cv BULK COLLECT INTO empnos, enames; CLOSE emp_cv; -- Bulk fetch with "implicit cursor" EXECUTE IMMEDIATE 'SELECT sal FROM emp WHERE ' || NVL (whr_in, '1=1') BULK COLLECT INTO sals; Dbms_Output.put_line (sals.COUNT); -- Bulk, dynamic UPDATE FORALL indx IN empnos.FIRST .. empnos.LAST EXECUTE IMMEDIATE 'UPDATE emp SET sal = sal * 1.1 WHERE empno = :employee_key ' || 'RETURNING ename INTO :names_updated' USING empnos(indx) -- Must specify individual row with FORALL index RETURNING BULK COLLECT INTO enames_updated; -- Specify collection as whole -- Using SQL%BULK_ROWCOUNT: how many rows modified FORALL indx IN ename_filter.FIRST .. ename_filter.LAST EXECUTE IMMEDIATE 'UPDATE emp SET sal = sal * 1.1 WHERE ename LIKE :employee_filter' USING ename_filter(indx); FOR indx IN ename_filter.FIRST .. ename_filter.LAST LOOP DBMS_OUTPUT.PUT_LINE ( 'Number of employees with names like "' || ename_filter(indx) || '" given a raise: ' || SQL%BULK_ROWCOUNT(indx)); END LOOP; END; /

PL/SQL Collections

3

by each statement?

PL/SQL Collections

4

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF