Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cursors Need Your Help! Please

Author  Topic 

mwendecker
Starting Member

3 Posts

Posted - 2014-04-28 : 21:29:22
I am trying to figure out how to work with cursors. Can someone guide me if this is correct.

--will select two rows
Cursor c_our_vend IS
SELECT UNIQUE vend_no
FROM out_hdr
WHERE hdr_no IN (SELECT UNIQUE hdr_no
FROM out_dtl
WHERE part_no = :ITEM item_no) -- a form field
AND co_no = :SYS$STANDARD_HDR. sys$co_no -- form field

BEGIN
OPEN our_vend;
FIRST_REC; -- do I need this since I have 2 records
--I want display the details
LOOP
FETCH cur_vend
INTO :D_VEND vend_no --form field
EXIT WHEN cur_vend%NOTFOUND

SELECT MAX(hdr_no)
INTO :D_VEND hdr_no --form field
FROM out_dtl
WHERE buyer_part = :ITEM.item_no
AND hdr_no IN (SELECT UNIQUE hdr_no
FROM out_hdr
WHERE part_no = :ITEM item_no)

SELECT curr_dt
INTO :D_VEND curr_dt -- form field
WHERE hdr_no = :D_VEND.hdr_no

NEXT_RECORD;
END LOOP;
FIRST_REC; --should I remove this since I seem to skip the 2nd rec?
CLOSE c_our_vend;
END;


Can someone help me to understand how I should loop through two distinct vendor numbers in my cursor? Any advice or sharing of knowledge is greatly appreciated.

Margie


mwendecker
Starting Member

3 Posts

Posted - 2014-04-28 : 23:13:24
I think I should omit the following from my code:
FIRST_REC; (1st time)
NEXT_REC;
FIRST_REC; (2nd time)

Any advice? - I am a newbie to this but I am determined to learn.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2014-04-29 : 06:31:50
SQLTeam is a Microsoft SQL Server website, your code looks like Oracle. There is an Oracle forum over at http://dbforums.com/ that can help you.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-04-29 : 09:08:22
quote:
Originally posted by mwendecker

I think I should omit the following from my code:
FIRST_REC; (1st time)
NEXT_REC;
FIRST_REC; (2nd time)

Any advice? - I am a newbie to this but I am determined to learn.



My advice is forget about cursors. Almost every problem can be solved with set-based logic and will run many times faster than a cursor-based solution.
Go to Top of Page
   

- Advertisement -