Solved

Is This Oracle Cursor Syntax Correct?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now