Home » SQL & PL/SQL » SQL & PL/SQL » Sys_refcursor
Sys_refcursor [message #608508] Thu, 20 February 2014 06:41 Go to next message
Rarawshenkiv
Messages: 5
Registered: February 2014
Location: Chennai
Junior Member
How to execute this procedure to get the output???


CREATE OR REPLACE PROCEDURE p1( TABLE_NAME IN varchar2, o OUT sys_refcursor)
AS

sqlstmt VARCHAR2(300);

BEGIN

sqlstmt := ' select * from '|| TABLE_NAME;

EXECUTE IMMEDIATE sqlstmt INTO o ;

END;

Re: Sys_refcursor [message #608510 is a reply to message #608508] Thu, 20 February 2014 06:46 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
hi,

please read the location:

https://community.oracle.com/thread/331109

[Updated on: Thu, 20 February 2014 06:46]

Report message to a moderator

Re: Sys_refcursor [message #616919 is a reply to message #608510] Mon, 23 June 2014 00:50 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
msol25 wrote on Thu, 20 February 2014 18:16
please read the location:

https://community.oracle.com/thread/331109


That link dooesn't seem to answer OP's question. It talks about the difference between ref cursor and explicit cursors.

@OP, Why do you want to unnecessarily complicate a simple select statement. What you are trying to achieve is 100% possible, however, not every possible thing is good to do.

1. You should not use dynamic sql for this purpose.
2. Table name should be static. Having said that, it means, you must always know the table name, and thus avoid using it dynamically.

Rarawshenkiv wrote on Thu, 20 February 2014 18:11
How to execute this procedure to get the output???


May be you should read documentation first and then go through Using Ref Cursors To Return Recordsets


Regards,
Lalit
Re: Sys_refcursor [message #616923 is a reply to message #608508] Mon, 23 June 2014 01:10 Go to previous message
Barbara Boehmer
Messages: 9088
Registered: November 2002
Location: California, USA
Senior Member
It would be better to change your procedure code slightly and use "OPEN o FOR sqlstmt;", as shown below, instead of "EXECUTE IMMEDIATE sqlstmt INTO o;".

SCOTT@orcl12c> CREATE OR REPLACE PROCEDURE p1
  2    (table_name IN  VARCHAR2,
  3  	o	   OUT SYS_REFCURSOR)
  4  AS
  5    sqlstmt	   VARCHAR2(300);
  6  BEGIN
  7    sqlstmt := 'SELECT * FROM ' || table_name;
  8    OPEN o FOR sqlstmt;
  9  END p1;
 10  /

Procedure created.

SCOTT@orcl12c> SHOW ERRORS
No errors.
SCOTT@orcl12c> VARIABLE g_ref REFCURSOR
SCOTT@orcl12c> EXEC p1 ('DEPT', :g_ref)

PL/SQL procedure successfully completed.

SCOTT@orcl12c> PRINT g_ref

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

4 rows selected.

[Updated on: Mon, 23 June 2014 01:11]

Report message to a moderator

Previous Topic: sysdate functioning
Next Topic: Procedure to read mails from mailbox and insert into table
Goto Forum:
  


Current Time: Fri Apr 19 02:58:10 CDT 2024