×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Cursors

Cursors

(OP)
I want the following SQL statements to be executed then update FOB.Purch_Ord_Line_No with the Purch_Ord_Line.Purch_Ord_Line_No.

I thought about using two cursors so that I could make sure of the FETCH NEXT. Below is an example of what data might be returned from the SELECT statements. I would then want to update 1 with 5, 2 with 6, 7 with 8, etc. Any suggestions?

Purch_Ord_Line   FOB
5                1
6                2
7                3
8                4

SELECT Purch_Ord_Line_No
FROM Purch_Ord_Line
WHERE Purch_Ord_No = 1887 AND Revision_No = 1

SELECT dbo.FOB.Purch_Ord_Line_No                           
FROM   dbo.FOB INNER JOIN
            dbo.Purch_Ord_Line ON dbo.FOB.Purch_Ord_Line_No = dbo.Purch_Ord_Line.Purch_Ord_Line_No AND dbo.Purch_Ord_Line.Purch_Ord_No = 1887 AND
            dbo.Purch_Ord_Line.Revision_No = 1



DECLARE PurchOrdLine_POL_Curs CURSOR
FOR

SELECT Purch_Ord_Line_No
FROM Purch_Ord_Line
WHERE Purch_Ord_No = 1887 AND Revision_No = 1
FOR READ ONLY
GO

DECLARE @POLNo Int
OPEN PurchOrdLine_POL_Curs

DECLARE FOB_POL_Curs CURSOR
FOR

SELECT  dbo.FOB.Purch_Ord_Line_No                                       
FROM    dbo.FOB INNER JOIN
            dbo.Purch_Ord_Line ON dbo.FOB.Purch_Ord_Line_No = dbo.Purch_Ord_Line.Purch_Ord_Line_No AND dbo.Purch_Ord_Line.Purch_Ord_No = 1887 AND
            dbo.Purch_Ord_Line.Revision_No = 1
FOR UPDATE
GO

DECLARE @FOBPOLNo Int
OPEN FOB_POL_Curs

FETCH NEXT FROM PurchOrdLine_POL_Curs INTO @POLNo
WHILE @@Fetch_Status = 0 BEGIN

FETCH NEXT FROM FOB_POL_Curs INTO @FOBPOLNo
WHILE @@Fetch_Status = 0 BEGIN
        UPDATE FOB
        SET Purch_Ord_Line_No = @POLNo
        WHERE CURRENT OF FOB_POL_Curs
    
FETCH NEXT FROM PurchOrdLine_POL_Curs INTO @POLNo
FETCH NEXT FROM FOB_POL_Curs INTO @FOBPOLNo

END

CLOSE PurchOrdLine_POL_Curs

DEALLOCATE PurchOrdLine_POL_Curs

CLOSE FOB_POL_Curs

DEALLOCATE FOB_POL_Curs

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close