Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
12Activity
0 of .
Results for:
No results containing your search query
P. 1
PL SQL Collection

PL SQL Collection

Ratings: (0)|Views: 1,236|Likes:
Published by writesmd
Oracle PLSQL Collections
Oracle PLSQL Collections

More info:

Published by: writesmd on Jul 16, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

02/21/2012

pdf

text

original

 
PL/SQL RECORD
A
record 
is a group of related data items stored in
fields
, each with its own name and datatype. Suppose youhave various data about an employee such as name, salary, and hire date. These items are logically related butdissimilar 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, youcannot specify the datatypes of fields in the record or declare fields of your own. The datatype RECORD liftsthose 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:
DECLARETYPE FiguresRec IS RECORD (cash REAL, notes REAL, ...);Sub1_figs FiguresRec;Sub2_figs FiguresRec;FUNCTION acid_test (figs FiguresRec) RETURN REAL IS...BEGINSELECT cash, notes ... INTO sub1_figs FROM assets, liabilitiesWHERE assets.sub = 1 AND liabilities.sub = 1;SELECT cash, notes ... INTO sub2_figs FROM assets, liabilitiesWHERE 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.
DECLARETYPE 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
 
BEGINOPEN C1;LOOPFETCH C1 INTO FLIGHT_INFO;EXIT WHEN C1%NOTFOUND;FOR I IN 1...FLIGHT_INFO.PASSENGERS.LAST LOOPIF FLIGHT_INFO.PASSENGERS (I).SEAT = ’NA’ THENDBMS_OUTPUT.PUT_LINE (FLIGHT_INFO.PASSENGERS (I).NAME);RAISE SEAT_NOT_AVAILABLE;END IF;...END LOOP;END LOOP;CLOSE C1;EXCEPTIONWHEN 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) intoindex-by table empnos. The previous values are overwritten.DECLARETYPE NUMTAB IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;CURSOR C1 IS SELECT EMPNO FROM EMP;EMPNOS NUMTAB;ROWS NATURAL := 10;BEGINOPEN 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)ISTYPE
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 CURSORemp_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 variableOPEN 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 UPDATEFORALL indx IN empnos.FIRST .. empnos.LASTEXECUTE 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 indexRETURNING BULK COLLECT INTO enames_updated; -- Specify collection as whole -- Using SQL%BULK_ROWCOUNT: how many rows modified by each statement?FORALL indx IN ename_filter.FIRST .. ename_filter.LASTEXECUTE IMMEDIATE'UPDATE emp SET sal = sal * 1.1WHERE ename LIKE :employee_filter'USING ename_filter(indx); FOR indx IN ename_filter.FIRST .. ename_filter.LASTLOOPDBMS_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

Activity (12)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
gangadhar1310 liked this
Amit Kasana liked this
uock44c liked this
enterpriya liked this
balaji liked this
dwilsondavid liked this
Yellanki liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->