mardi 12 juillet 2016

SQL Cursor Compare Previous Value

I want to use cursor to go through the entire line in the table to find the heaviest weight. This is what I got so far that presents the right answer but how do I make it such that it compares the Current Weight with the Previous Weight, and let the cursor print out the heaviest weight? Thanks :)

DECLARE @weight DECIMAL(8,2),
@name VARCHAR(100);

DECLARE Cursor_Eight CURSOR
FOR
SELECT Name, Weight
    FROM [SalesLT].[Product]
    ORDER BY Weight DESC;

OPEN Cursor_Eight

FETCH NEXT FROM Cursor_Eight INTO @name, @weight
PRINT @name + ' with a weight of ' + CONVERT(CHAR(8),@weight) + ' is the heaviest product.';

CLOSE Cursor_Eight 

DEALLOCATE Cursor_Eight

Aucun commentaire:

Enregistrer un commentaire