Solved

Is This Oracle Cursor Syntax Correct?

Posted on 2013-01-09
4
445 Views
Last Modified: 2013-03-05
I need someone to review my attempt at an Oracle cursor.  I am attempting to loop through a series of tables to find employee numbers that exist in a temp table:
DECLARE

cursor empcur
IS
SELECT employee_num from tmp_employeenums
BEGIN
OPEN empcur
LOOP
fetch empcur into emp_record
exit when empcur%notfound;

SELECT * from vacationdays where cavempnum = emp_record;

SELECT * from sickdays where sicempnum = emp_record;

commit;
end loop;

end;
0
Comment
Question by:programmher
[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:
Steve Wales earned 125 total points
ID: 38758865
Your syntax is basically correct, except you've forgotten semi colons almost everywhere :)

You also need to declare the variables you're using.

Also, your selects at the end need to have their output selected into a variable.

I created the three tables you mentioned.

tmp_employeenum had one column containing employee_numbers.
vacationdays had 2 columns: employee_num and a numeric column called hours.
sickdays was the same layout as vacations days.

The following code executed successfully (after doing set serveroutput on).:
DECLARE
emp_record vacationdays.cavempnum%type;
vacdays number;
sicdays number;
emp vacationdays.cavempnum%type;

cursor empcur
IS
SELECT employee_num from tmp_employeenums;
BEGIN
OPEN empcur;
LOOP
fetch empcur into emp_record;
exit when empcur%notfound;

SELECT cavempnum, hours into emp, vacdays from vacationdays where cavempnum = emp_record;

dbms_output.put_line('Vacation for '||emp||': '||to_char(vacdays));

SELECT sicempnum, hours into emp, sicdays from sickdays where sicempnum = emp_record;

dbms_output.put_line('Sick for '||emp||': '||to_char(sicdays));

commit;
end loop;

end; 
/

Open in new window


You would need to then do whatever it was you wanted to do with the output

Oh, and the commit isn't necessary, you're not making any changes.

Finally, here's a couple of references on a couple of other ways to do cursors:

http://sql-plsql.blogspot.com/2007/05/examples-of-oracle-plsql-cursors.html
http://www.techonthenet.com/oracle/loops/cursor_for.php
0
 
LVL 35

Expert Comment

by:johnsone
ID: 38758931
I prefer to consolidate and not declare the cursor.  Using the for loop syntax it does a lot for you in this type of situation.  The code comes down to this:
declare
  vacdays vacationdays%rowtype;
  sicdays sicdays%rowtype;
begin
  for cursor_rec in (SELECT employee_num from tmp_employeenums) loop
    select * into vacdays from vacationdays where cavempnum = cursorrec.employee_num;
    select * into sicdays from sickdays where sicempnum = cursorrec.employee_num;
  end loop;
end;
/

Open in new window

That is typed in and I did not actually run it, but it should  be close to the original.
0
 

Author Comment

by:programmher
ID: 38758978
Thank you so much!  I'm making the changes and running the script on the test database.

I also was just informed I need to search against a table in a totally different database.  Is this syntax correct:

SELECT employee_num
FROM STDIS
where exists (SELECT mm_employee_num@database_link_to_hr_info B WHERE stdis.employee_num = b.employee_num);
0
 
LVL 35

Expert Comment

by:johnsone
ID: 38758995
No, you are missing a table name in your subquery.  It should look more like this:

SELECT employee_num
FROM STDIS
where exists (SELECT mm_employee_num from table_name@database_link_to_hr_info B WHERE stdis.employee_num = b.employee_num);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

738 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