Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle SQL Cursor

Posted on 2012-12-28
11
Medium Priority
?
311 Views
Last Modified: 2013-10-27
Ok, so tell me why this is failing below.  



SET SERVEROUTPUT ON

DECLARE

      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201003';
  i INTEGER
 
 
  OPEN testcursor;
     LOOP
        i := i + 1;

        insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2);
        EXIT WHEN c1%NOTFOUND;
        -- process data record
     END LOOP;
     CLOSE testcursor;
  END;
0
Comment
Question by:synikaldemon
10 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 38727045
are you getting any error?

However, the cursor query needs some modification as it contains both date and time

I suspect you are looking only year and month in eff_date, if not, do let us know

SELECT *
            FROM m_old_test
            WHERE to_date(eff_date, 'YYYYMM') = '201003';

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727068
EXIT WHEN c1%NOTFOUND;
must be first line in the loop?


SET SERVEROUTPUT ON

DECLARE

      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201003';
  i INTEGER
 
 
  OPEN testcursor;
     LOOP
        EXIT WHEN c1%NOTFOUND;

        -- process data record
        i := i + 1;

        insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2);
     END LOOP;
     CLOSE testcursor;
  END; 

Open in new window


so far, you don't need any cursor actually, a plain "insert" would do
insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
select XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2
  from  m_old_test
     WHERE eff_date = '201003'

Open in new window


and in case the "failure" is because eff_date is date field, you may need to use the proper syntax:

     WHERE eff_date >= TO_DATE('201003', 'YYYYMM')
         AND eff_date < TO_DATE('201004', 'YYYYMM')

read up:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
0
 

Author Comment

by:synikaldemon
ID: 38727131
Below is the error message.  For the date, we call it eff_date, but it is actually a string.  I don't think that is the problem.  Basically we are just trying to grab a bunch of records from one table and move them to another with the same structure.  



----------------------------------------------------------------------------------


Error starting at line 3 in command:


DECLARE

      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201103';
  i INTEGER
 
 
  OPEN testcursor;
     LOOP
        i := i + 1;
       
   
 insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2);

        EXIT WHEN c1%NOTFOUND;
     
     END LOOP;
     CLOSE testcursor;
  END;


Error report:
ORA-06550: line 10, column 3:
PLS-00103: Encountered the symbol "OPEN" when expecting one of the following:

   := . ( @ % ; not null range default character
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727154
you are missing the ; after INTEGER and the BEGIN keyword:

DECLARE
      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201103';
  i INTEGER; 
 BEGIN
  OPEN testcursor;
     LOOP
        EXIT WHEN c1%NOTFOUND;

        i := i + 1;
       
        insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2);

     
     END LOOP;
     CLOSE testcursor;
  END;

Open in new window

0
 

Author Comment

by:synikaldemon
ID: 38727165
Yep angelIII we don't need a cursor for this, but we need to do some other things with the data, so we are just trying to get a cursor set up correctly.  Thanks for your help, you too ajexpert.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727177
>but we need to do some other things with the data
you must be doing something really "crazy" that you really need a cursor..
SQL is very powerful (though not almighty)
but that is another question :)
0
 

Author Comment

by:synikaldemon
ID: 38727478
What I am getting now

Error starting at line 3 in command:
DECLARE
      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201103';
  i INTEGER;
 BEGIN
  OPEN testcursor;
     LOOP
        EXIT WHEN testcursor%NOTFOUND;

        i := i + 1;

    insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2);

     
     END LOOP;
     CLOSE testcursor;
  END;
 
Error report:
ORA-06550: line 15, column 45:
PL/SQL: ORA-00984: column not allowed here
ORA-06550: line 14, column 10:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38727610
sure... you also need a FETCH statement:
http://psoug.org/reference/control_struct.html

DECLARE
      CURSOR testcursor IS
            SELECT *
            FROM m_old_test
            WHERE eff_date = '201103';
  i INTEGER;
  rec m_old_tet%ROWTYPE;
 BEGIN
  OPEN testcursor;
     LOOP
        FETCH testcursor INTO rec;
        EXIT WHEN testcursor%NOTFOUND;

        i := i + 1;

    insert into m_old_test1 (XXCE, XXAA,XXBA,XXA1,XXAG,ACRO,ACRO2)
        values(rec.XXCE, rec.XXAA,rec.XXBA,rec.XXA1,rec.XXAG,rec.ACRO,rec.ACRO2);

     
     END LOOP;
     CLOSE testcursor;
  END;

Open in new window

0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 38730267
Yes, the "fetch" line is required *IF* you want to do this in PL\SQL.  But, I don't see any reason here to use PL\SQL.  Why not just do:
Insert into m_oldtest1
select * from m_oldtest
where eff_date = '201103';

Also, I just noticed this problem in your procedure: you are using the column names from m_oldtest1 to provide the values for the insert into the same table!  That will never give you what you want.
0
 
LVL 23

Expert Comment

by:Steve Wales
ID: 39603599
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

783 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