Solved

Cursor using Dynamic SQL

Posted on 2002-03-28
4
1,813 Views
Last Modified: 2012-06-27
Hi,

It is not possible to create a cursor using Dynamic SQL in Oracle (ver 8.1.5). When I try to do this, I get the following error message.

PLS-00455: cursor cannot be used in dynamic SQL OPEN statement

Is there any work around for this?

Regards
SV1040
0
Comment
Question by:sv1040
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
DrSQL earned 50 total points
ID: 6902511
sv1040,
    I have two answers.  First, are you using the RETURNS clause, because that isn't how you get rows fetched into your program.  The RETURNS is for getting the results of a delete, update, or other sql command that returns a count or value (like a function).  Since you're OPENning the cursor, it knows it's a select and can't have a return.  If you want rowcount, you'll have to handle that separately from the OPEN.  The rest of my answer may be painfully obvious, but just in case...

  For Dynamic SQL (open-for or DBMS_SQL) you use a reference cursor or a number and apply it in the open for or DBMS_SQL.OPEN.  Here's some examples:

OPEN/FOR:

DECLARE
   TYPE EmpCurTyp IS REF CURSOR;  -- define weak REF CURSOR type
   emp_cv   EmpCurTyp;  -- declare cursor variable
   my_ename VARCHAR2(15);
   my_sal   NUMBER := 1000;
BEGIN
   OPEN emp_cv FOR  -- open cursor variable
      'SELECT ename, sal FROM emp WHERE sal > :s' USING my_sal;
   ...
END;


DBMS_SQL:

Consider a table MULTI_TAB defined as:

create table multi_tab (num number,
                        dat1 date,
                        var varchar2(24),
                        dat2 date)


To select everything from this table and move it into four PL/SQL tables, you could use the following simple program:

declare
  c       number;
  d       number;
  n_tab  dbms_sql.Number_Table;
  d_tab1 dbms_sql.Date_Table;
  v_tab  dbms_sql.Varchar2_Table;
  d_tab2 dbms_sql.Date_Table;
  indx number := 10;
begin

  c := dbms_sql.open_cursor;
  dbms_sql.parse(c, 'select * from multi_tab order by 1', dbms_sql);

  dbms_sql.define_array(c, 1, n_tab,  5, indx);
  dbms_sql.define_array(c, 2, d_tab1, 5, indx);
  dbms_sql.define_array(c, 3, v_tab,  5, indx);
  dbms_sql.define_array(c, 4, d_tab2, 5, indx);

  d := dbms_sql.execute(c);

  loop
    d := dbms_sql.fetch_rows(c);

    dbms_sql.column_value(c, 1, n_tab);
    dbms_sql.column_value(c, 2, d_tab1);
    dbms_sql.column_value(c, 3, v_tab);
    dbms_sql.column_value(c, 4, d_tab2);
 
    exit when d != 5;
  end loop;

  dbms_sql.close_cursor(c);


Good luck!
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7063101
Please update and finalize this old, open question. Please:

1) Award points ... if you need Moderator assistance to split points, comment here with details please or advise us in Community Support with a zero point question and this question link.
2) Ask us to delete it if it has no value to you or others
3) Ask for a refund so that we can move it to our PAQ at zero points if it did not help you but may help others.

EXPERT INPUT WITH CLOSING RECOMMENDATIONS IS APPRECIATED IF ASKER DOES NOT RESPOND.

Thanks,

** Mindphaser - Community Support Moderator **
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7082297
Recommended disposition:

    Accept DrSQL's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7096029
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now