0

I have used sys_refcursor before to return the data of select statements.But,I am using execute immediate here,so I am not able to use the sys_refcursor in execute immediate statement.

Before I used to do

R_C OUT SYS_REFCURSOR //refcursor
    OPEN R_C FOR  //it used to handle the return of all select statement
    SELECT * FROM table_name;

But ,I tried execute immediate today and I am not able to print the data of select statement. So,following I tried is:

CREATE OR REPLACE PROCEDURE OT.check_data(DATA1 number,R_C OUT SYS_REFCURSOR)
    IS 
    vquery long;
    BEGIN
    vquery :='select * from ot.employee ';
    if data1 = 10 then
    vquery := vquery||' where deptno in (10)';
    else
     vquery := vquery||' where deptno in (20,30)';
    end if;
    execute immediate vquery;
    END;
    /

   exec OT.check_data(10); 

The procedure is working fine,but I am unable to see the data of select statement.How can I display the data of execute immediate in my console?I am using toad.

2 Answers 2

3

You can return a SYS_REFCURSOR for a dynamically built SELECT statement by doing the following:

CREATE OR REPLACE PROCEDURE OT.check_data(DATA1    number,
                                          R_C OUT  SYS_REFCURSOR)
IS 
  vQuery VARCHAR2(32767);
  rc     SYS_REFCURSOR;
BEGIN
  vQuery :='select * from ot.employee ';

  if data1 = 10 then
    vQuery := vQuery ||' where deptno in (10)';
  else
    vQuery := vQuery ||' where deptno in (20,30)';
  end if;

  OPEN rc FOR vQuery;
  R_C := rc;
END;

I strongly suggest that you should not use the LONG datatype. This has been deprecated for years, and One Of These Days (tm) Oracle is finally going to drop support for it. Either use a VARCHAR2, which is more than sufficient in this case, or use a CLOB.

Sign up to request clarification or add additional context in comments.

4 Comments

[Warning] ORA-24344: success with compilation error 6/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: := . ( @ % ; not null range default character The symbol ";" was substituted for "BEGIN" to continue. (1: 0): Warning: compiled but with compilation errors
Missed a semicolon in the SYS_REFCURSOR line.
I tried to execute the procedure using VARIABLE R_C REFCURSOR; exec OT.check_data(10,R_C); but it is saying me PLS-00201: identifier 'R_C' must be declared. what is the mistake i Did?
VARIABLE references must be preceded by a colon. So :R_C instead of R_C.
0

You can create a simple procrdure for it.

CREATE OR REPLACE PROCEDURE OT.check_data(DATA1    number,
                                          R_C OUT  SYS_REFCURSOR)
IS 
BEGIN
  Open R_C for 'select * from ot.employee where deptno in ('
               || case when data1 = 10 then '10' 
                       Else '10,20' end
               || ')';

END;
/

You can call the procedure like this:

Declare
Rc sys_refcursor;
Begin
Ot.check_data(10, rc);
-- do something with returned cursor
End;
/

You can print or use cursor data in pl/sql block: see this qna

Cheers!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.