Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Workaround for opening a strongly typed cursor using native dynamic SQL

289848Jun 10 2002
Hi All,

In reading the PL/SQL documentation for Oracle 9i, I noted that the OPEN-FOR
statement with a dynamic SQL string only allows the use of weakly typed cursors.
I have verified this limitation with my own experimentation as follows:

DECLARE
type rec_type is record(
str varchar2(40),
num number(22)
);
type cur_type is ref cursor return rec_type;
my_cur cur_type;
que varchar2(100);
tab varchar2(40);
BEGIN
tab := 'dynamic_table_name';
que := 'select key_name, key_value from ' || tab || ' where key_name like ''01%''';
open my_cur for que;
loop
if my_cur%found then
dbms_output.put_line('source_name: ' || my_cur.str || ', page_sn: ' || my_cur.num);
exit;
end if;
end loop;
close my_cur;
END;

Running the above trivial example in an anonymous sql block yields the following
errors as expected:

ORA-06550: line 10, column 8:
PLS-00455: cursor 'MY_CUR' cannot be used in dynamic SQL OPEN statement
ORA-06550: line 10, column 3:
PL/SQL: Statement ignored
ORA-06550: line 13, column 54:
PLS-00487: Invalid reference to variable 'MY_CUR'
ORA-06550: line 13, column 7:
PL/SQL: Statement ignored

Is there a workaround to the situation? Since I do not know the table name at run
time, I must use Native Dynamic SQL. I have a long and complex record type
that I wish to return through JDBC using the REFCURSOR Oracle type in order to
avoid having to register an inordinate number of OUT parameters. Moreover, I
would like to return potentially one or more results in a ResultSet. Using the
standard method of registering native SQL types for the IN and OUT bindings
can only return one result. Hence the reason I would like to return a strong
cursor type. Also, the type of query I am doing is complex, and needs to be
executed in a PL/SQL procedure for performance reasons. Therefore simply
executing a SELECT query dynamically built up on the the JDBC client won't
do the trick.

If anybody has experience with a similar problem and would like to volunteer
information on their workaround, I would really appreciate it.

Best Regards,

J. Metcalf

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2002
Added on Jun 10 2002
3 comments
173 views