Solved

Is This Oracle Cursor Syntax Correct?

Posted on 2013-01-09
4
446 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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.
Suggested Courses

632 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