Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is This Oracle Cursor Syntax Correct?

Posted on 2013-01-09
4
Medium Priority
?
447 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 23

Accepted Solution

by:
Steve Wales earned 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

730 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