4
GO
SET NOCOUNT ON;

DECLARE
@idAdvertisements int,
@Name nvarchar(255),
@Description nvarchar(500),
@DepartureDate datetime,
@Cities_idCities int,
@Areas_idAreas int,
@Countries_idCountries int,
@Agencies_idAgencies int,
@Url nvarchar(1000),
@Price decimal(6, 2),
@HollidayDuration int,
@BookingDate datetime;

DECLARE ad_cursor CURSOR
   FOR SELECT idAdvertisements
             ,Name
             ,Description
             ,DepartureDate
             ,Cities_idCities
             ,Areas_idAreas
             ,Countries_idCountries
             ,Agencies_idAgencies
             ,Url
             ,Price
             ,HollidayDuration
             ,BookingDate
      FROM Advertisements;

OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO @idAdvertisements
    ,@Name
    ,@Description
    ,@DepartureDate
    ,@Cities_idCities
    ,@Areas_idAreas
    ,@Countries_idCountries
    ,@Agencies_idAgencies
    ,@Url
    ,@Price
    ,@HollidayDuration
    ,@BookingDate;

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT ' ';
   PRINT @idAdvertisements;
   --PRINT @Name;
   --PRINT @Description;
   --PRINT @DepartureDate;
   --PRINT @Cities_idCities;

END
CLOSE ad_cursor; 

But I always get 1, 1, 1, 1, 1. Data are always the same.

SELECT statement is OK. I don't understand why. Can someone see the problem?

5
  • 5
    Why even bother with a CURSOR?? If you must use a CURSOR, then at least define it as CURSOR FAST_FORWARD to speed it up a little. But the best choice would be to avoid the cursor alltogether - in 90% of the cases, you can, too! Commented Jul 22, 2010 at 12:04
  • agree with marc_s there is no need for cursor for that. Commented Jul 22, 2010 at 12:07
  • more code inside so curosr is needed. I delete some code for better view. Commented Jul 22, 2010 at 12:09
  • Let me increase mark_s's 90% with 9. @senzacionale: More code does not mean cursors are more reasonable. Commented Jul 22, 2010 at 12:21
  • suggest you read this before trying to write another cursor, cursor are the worst way to do things in many databases! wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them Commented Jul 22, 2010 at 18:11

3 Answers 3

3

cursors are Evil.. eVIL.. eVil.. stay away from them

Now, here is the issue - you are not moving the cursor forward..

DECLARE ad_cursor CURSOR
   FOR SELECT idAdvertisements, Name, Description, DepartureDate, Cities_idCities, Areas_idAreas,
    Countries_idCountries, Agencies_idAgencies, Url, Price, HollidayDuration, BookingDate FROM Advertisements;

OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO @idAdvertisements, @Name, @Description, @DepartureDate, @Cities_idCities, @Areas_idAreas,
@Countries_idCountries, @Agencies_idAgencies, @Url, @Price, @HollidayDuration, @BookingDate;

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT ' ';
   PRINT @idAdvertisements;
   --PRINT @Name;
   --PRINT @Description;
   --PRINT @DepartureDate;
   --PRINT @Cities_idCities;

    FETCH NEXT FROM ad_cursor
    INTO @idAdvertisements, @Name, @Description, @DepartureDate, @Cities_idCities, @Areas_idAreas,
    @Countries_idCountries, @Agencies_idAgencies, @Url, @Price, @HollidayDuration, @BookingDate;


END
CLOSE ad_cursor; 
Sign up to request clarification or add additional context in comments.

Comments

3

The code as you have it here will loop infinitely. You need another FETCH at the end of the body of the WHILE loop, otherwise @@FETCH_STATUS will never change.

Comments

2
OPEN ad_cursor;
FETCH NEXT FROM ad_cursor
INTO @idAdvertisements, @Name, @Description, @DepartureDate, @Cities_idCities, @Areas_idAreas,
@Countries_idCountries, @Agencies_idAgencies, @Url, @Price, @HollidayDuration, @BookingDate;

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT ' ';
   PRINT @idAdvertisements;
   --PRINT @Name;
   --PRINT @Description;
   --PRINT @DepartureDate;
   --PRINT @Cities_idCities;
FETCH NEXT FROM ad_cursor
INTO @idAdvertisements, @Name, @Description, @DepartureDate, @Cities_idCities, @Areas_idAreas,
@Countries_idCountries, @Agencies_idAgencies, @Url, @Price, @HollidayDuration, @BookingDate;
END
CLOSE ad_cursor; 

You have add fetch the next row, just before the END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.