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
 ? on Cursor

Author  Topic 

zwheeler
Starting Member

25 Posts

Posted - 2012-01-31 : 17:23:37
Hi
I have a cursor and I am trying to understand it. I have a couple of questions. here is the beginning of the cursor

DECLARE Cur1 CURSOR FAST_FORWARD FOR
SELECT fk_property_id, date_property_event, date_age_out
FROM stg_mddr_assignment
ORDER BY fk_property_id, date_property_event, date_age_out
DECLARE @pid int, @date_start datetime
DECLARE @pid_A int, @date_start_A datetime, @date_end_A datetime
DECLARE @pid_B int, @date_start_B datetime, @date_end_B datetime
OPEN Cur1
FETCH NEXT FROM Cur1 INTO @pid_A, @date_start_A, @date_end_A
FETCH NEXT FROM Cur1 INTO @pid_B, @date_start_B, @date_end_B

Question:
FETCH NEXT FROM Cur1 INTO @pid_A, @date_start_A, @date_end_A
FETCH NEXT FROM Cur1 INTO @pid_B, @date_start_B, @date_end_B

When the sql is executed for Cur1
does this contain the same rows or does the first fetch get row 1 and puts row 1 data into _A variables and the second fetch gets row 2 data and puts it into variables _B

or do they both contain the same data i.e. Row1

Thanks in advance

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-01-31 : 17:45:59
quote:
the first fetch get row 1 and puts row 1 data into _A variables and the second fetch gets row 2 data and puts it into variables _B
That. FETCH NEXT moves the row pointer of the cursor as long as there are rows to be fetched.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-31 : 18:10:56
I hope your 2nd question is: how do I rewrite this without using a cursor?

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-31 : 19:09:45
seems like whole exercise is to somekind of comparison between consecutive rows. why should you use cursor for that?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -