Solved

Oracle SQL Cursor

Posted on 2012-12-28
11
299 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
[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
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 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

751 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