Solved

Is This Oracle Cursor Syntax Correct?

Posted on 2013-01-09
4
444 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
  • 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 34

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 34

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

830 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