0% found this document useful (0 votes)
61 views

Oracle Collections Practical Example

The document describes two scenarios that create and call stored procedures in Oracle. Scenario 1 creates object, collection, and procedure types to populate a collection from a table and return it through a cursor. Scenario 2 creates a simpler procedure that directly outputs matching department names without using collections or cursors. Both scenarios call the procedures in an anonymous block, passing a collection of department numbers to retrieve and output the corresponding name for each number.

Uploaded by

Srinivas Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Oracle Collections Practical Example

The document describes two scenarios that create and call stored procedures in Oracle. Scenario 1 creates object, collection, and procedure types to populate a collection from a table and return it through a cursor. Scenario 2 creates a simpler procedure that directly outputs matching department names without using collections or cursors. Both scenarios call the procedures in an anonymous block, passing a collection of department numbers to retrieve and output the corresponding name for each number.

Uploaded by

Srinivas Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SCENARIO 1:

CREATE OBJECT TYPE:


CREATE OR REPLACE TYPE ROW_TYPE as object (name varchar2(20));

CREATE COLLECTION TYPE:


CREATE OR REPLACE TYPE TAB_TYPE IS TABLE OF ROW_TYPE;

CREATE COLLECTION TYPE:


CREATE OR REPLACE TYPE V_NUM IS TABLE OF NUMBER(10);

CREATE NEW PROCEDURE:


CREATE OR REPLACE PROCEDURE PROC1 (P1_TYPE V_NUM, V_REF_DNAME OUT SYS_REFCURSOR)
IS
A2_TYPE TAB_TYPE := TAB_TYPE();
BEGIN
IF P1_TYPE.COUNT > 0 THEN
FOR I IN 1 .. P1_TYPE.COUNT LOOP
FOR J IN (SELECT DNAME FROM DEPT WHERE DEPTNO=P1_TYPE(I)) LOOP
A2_TYPE.EXTEND;
A2_TYPE( A2_TYPE.LAST) := ROW_TYPE(J.DNAME);
END LOOP;
END LOOP;
END IF;
OPEN V_REF_DNAME FOR SELECT * FROM TABLE(CAST(A2_TYPE AS TAB_TYPE));
END;

CALL PROCEDURE IN ANONYMOUS BLOCK:


DECLARE
P1_TYPE V_NUM;
V_REF_DNAME SYS_REFCURSOR;
V_DNAME VARCHAR2(20);
BEGIN
P1_TYPE := V_NUM();
P1_TYPE.EXTEND(4);
P1_TYPE(1):=10;
P1_TYPE(2):=20;
P1_TYPE(3):=30;
P1_TYPE(4):=40;
PROC1(P1_TYPE,V_REF_DNAME);
LOOP
FETCH V_REF_DNAME INTO V_DNAME;
EXIT WHEN V_REF_DNAME%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_DNAME);
END LOOP;
END;

OUTPUT:
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SCENARIO 2:
CREATE PROCEDURE:
CREATE OR REPLACE PROCEDURE PROC1 (P1_TYPE V_NUM)
IS
BEGIN
IF P1_TYPE.COUNT > 0 THEN
FOR I IN 1 .. P1_TYPE.COUNT LOOP
FOR J IN (SELECT DNAME FROM DEPT WHERE DEPTNO=P1_TYPE(I)) LOOP
DBMS_OUTPUT.PUT_LINE(J.DNAME);
END LOOP;
END LOOP;
END IF;
END;

CALL PROCEDURE IN ANONYMOUS BLOCK:


DECLARE
P1_TYPE V_NUM;
BEGIN
P1_TYPE := V_NUM();
P1_TYPE.EXTEND(4);
P1_TYPE(1):=10;
P1_TYPE(2):=20;
P1_TYPE(3):=30;
P1_TYPE(4):=40;
PROC1(P1_TYPE);
END;

OUTPUT:
ACCOUNTING
RESEARCH
SALES
OPERATIONS

You might also like