Solved

Oracle SQL Cursor

Posted on 2012-12-28
11
288 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
11 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 142

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
 
LVL 142

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 34

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 22

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 78
Oracle Database Upgrade 13 41
System.InvalidCastException: Specified cast is not valid 10 48
sql query Help 12 29
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

757 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

18 Experts available now in Live!

Get 1:1 Help Now