Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cursor using Dynamic SQL

Posted on 2002-03-28
4
Medium Priority
?
1,914 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
DrSQL earned 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

670 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