The following example illustrates how to convert a result
set, created from a dynamic DML statement, to a reference cursor.
In 11g Release 2, the converted reference cursor can then be passed, as a parameter, onto a Java class or any PL/SQL procedure for further processing. In 11g Release 1, the reference cursor could only be passed as a parameter to PL/SQL procedures.
Firstly, create a test table with some sample data;
create table Trade_Deal
(Deal_ID number not null
,Deal_Type varchar2(10)
,Deal_Version integer
,Created timestamp default current_timestamp
,Trade_Risk integer check (Trade_Risk in (1,2,3,4,5,6,7,8,9))
,GBP_Amnt number not null
,USD_Amnt number);
insert all
into Trade_Deal values(1,'SPOT', 1,current_timestamp,1,10,null)
into Trade_Deal values(2,'FORWARD',1,current_timestamp,1,12,null)
into Trade_Deal values(3,'DEPOSIT',1,current_timestamp,1,11,null)
into Trade_Deal values(4,'SPOT', 1,current_timestamp,1,22,null)
select 1
from dual
/
Next, create a procedure to test with;
create or replace
procedure Get_Deals(p_Deal_Type in varchar2
,p_Ref out sys_refcursor) as
v_SQL clob;
v_Cursor binary_integer := dbms_sql.open_cursor;
v_Ref sys_refcursor;
v_Exec binary_integer;
begin
-- Note, there is no reason to execute the following SELECT as a
-- dynamic statement, the reason for doing so is simply
-- to illustrate that a reference cursor can be pointed to a
-- result set using the DBMS_SQL API.
v_SQL := 'select td.Deal_ID
,td.Deal_Type
,td.Created
,td.Trade_Risk
,td.GBP_Amnt
from
Trade_Deal td
where
td.Deal_Type = :DT';
dbms_sql.parse(v_Cursor, v_SQL, dbms_sql.native);
dbms_sql.bind_variable(v_Cursor, 'DT', p_Deal_Type);
v_Exec := dbms_sql.execute(v_Cursor);
-- Convert to a REF cursor after execution
v_Ref := dbms_sql.to_refcursor(v_Cursor);
p_Ref := v_Ref;
end;
/
Now for the test;
SQL> variable test_Ref refcursor;
SQL> set serveroutput on
SQL> begin
2 Get_Deals('SPOT', :Test_Ref);
3 end;
4 /
PL/SQL procedure successfully completed.
Verfify the results;
SQL> print :Test_Ref;
DEAL_ID DEAL_TYPE CREATED TRADE_RISK GBP_AMNT
------- --------- ------------------------- ---------- --------
1 SPOT 16-NOV-11 11.04.57.957000 1 10
4 SPOT 16-NOV-11 11.04.57.957000 1 22
I use this blog as a dumping ground for various scripts as well as solutions to Maths problems.
Labels
- Alter (2)
- Analytic functions (2)
- AQ (2)
- AWR (4)
- Collections (2)
- Connect By (2)
- Constraints (7)
- Date/Time calculations (3)
- Dynamic SQL (4)
- Explain Plan (1)
- Formatting (3)
- Functions (1)
- Generating Functions (1)
- Indexes (7)
- Insert (3)
- JMS (3)
- Joins (4)
- LOB (3)
- locking (5)
- Partitions (3)
- Performance (10)
- Security (1)
- SQL Plus (3)
- Tabibitosan (1)
- Triggers (1)
- Views (1)
- XML (4)
Script categories
- Performance (10)
- Constraints (7)
- Indexes (7)
- locking (5)
- AWR (4)
- Dynamic SQL (4)
- Joins (4)
- XML (4)
- Date/Time calculations (3)
- Formatting (3)
- Insert (3)
- JMS (3)
- LOB (3)
- Partitions (3)
- SQL Plus (3)
- AQ (2)
- Analytic functions (2)
- Collections (2)
- Connect By (2)
- Explain Plan (1)
- Functions (1)
- Security (1)
- Triggers (1)
- Views (1)
Thursday, 17 November 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment