Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can REF cursor be opened dynamically?

Status
Not open for further replies.

BADCODER

Programmer
Sep 29, 2003
11
US
This is the declartion:
------------------------

TYPE CTyp IS REF CURSOR;
cur CTyp;


This the Error LINE:
-----------------------

v_open:= 'BEGIN OPEN Cur '||' FOR '||''''||v_final_str||''''||' USING '|| p1_id ||','|| p2_id||','|| p3_id||'; END;';

EXECUTE IMMEDIATE v_open;


This is the ERROR:
----------------------

PLS-00201: identifier 'CUR' must be declared


PS: I also tried using
'BEGIN OPEN '|| Cur || ....
but, Cur type cannot be concatenated to string.
Also everything works OK if Dynamic is not used, but since i dont know the id's ahead of time, I have to use dynamic.

 
What's in v_final_str? Some kind of SQL statement? I think you need you build it up incorporating the values of p1_id and p2_id, rather than appending that "USING" command (that I can't find in the Oracle documentation, btw).

Then you can do something like this...
[tt]
v_final_str := 'SELECT ename FROM emp WHERE empno BETWEEN '||
p1_id||' AND '||p2_id;

OPEN cur FOR v_final_str;
FETCH cur INTO v_name;
-- ... more code here ...
CLOSE cur;
[/tt]

-- Chris Hunt
 
Thanks. That's exactly the way v_final_str has been built. Sutle difference, p1_id anf p2_id are bind variables- :p1_id, :p2_id.

Now the Ref cursoe is weakly typed, as it has to be opened every time for a new query, similar to above. Thed bind variable get their values from "USING p1id, p2id which are(2 parameters out of 5, passed to the procedure which contains this v_final_str).
Different parameters (out of 5) are used each time for opening the REF cursor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top