Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

T-SQL Cursor doesn't move to next 1

Status
Not open for further replies.

VeryGoodGirl

Programmer
May 9, 2007
7
US
Below is a simple T-sql cursor. It seems to get stuck on the first row. What am I doing wrong?


USE ADatabase
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'pr_Update_Aircraft' AND type = 'P')
DROP PROCEDURE pr_Update_Aircraft

go
CREATE PROCEDURE pr_Update_Aircraft AS
DECLARE
@AircraftID int,
@AircraftStatusID int,
@tail_num varchar(10)

DECLARE AirCraft_Cursor CURSOR FOR
SELECT AircraftID,AircraftStatusID,aircrafttailshort
FROM sharon_AcAircraft


OPEN AirCraft_Cursor

FETCH NEXT FROM AirCraft_Cursor
INTO
@AircraftID,
@AircraftStatusID,
@tail_num

While @@FETCH_STATUS = 0
BEGIN
print ' in loop, status is : ' + @tail_num

END



-- It takes so long and when I kill it, it has only printed for the first record.
 
In spite of the fact that I think cursors are the spawn of Satan:

You need to FETCH NEXT after your code statements inside the BEGIN - END block execute to get the thing to move:

Code:
OPEN cur_Curses  
  
FETCH NEXT FROM cur_Curses   
    INTO @accountId, @fromDate, @toDate
WHILE @@FETCH_STATUS = 0  
BEGIN  

-- some code occurs...
 
    FETCH NEXT FROM cur_Curses   
        INTO @accountId, @fromDate, @toDate
END

< M!ke >
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top