Si vas a SAN GOOGLE y buscas, encontraras ejemplos, como este:
1. Create a temp table with an identity column that auto-increments, starting at 1 and going up by 1.
Código SQL:
Ver originalCREATE TABLE #Temp1 (
ID INT IDENTITY(1,1),
ItemName VARCHAR(50),
ItemDescription VARCHAR(50)
) .
2. Get the data inserted for review (you do not need to insert the ID field, it is being inserted with numbers increasing by 1 and starting at 1).
Código SQL:
Ver originalINSERT INTO #Temp1(
ItemName,
ItemDescription
)
SELECT ItemName,
ItemDescription
FROM InventoryTable .
3. Create the loop and cycle through the data.
Código SQL:
Ver originalDECLARE @Counter INT
SET @Counter = 1
DECLARE @ItemReview VARCHAR(50)
WHILE @Counter <= (SELECT COUNT(*) FROM InventoryTable)
BEGIN
SELECT @ItemReview = t.ItemName
FROM #Temp1 t
WHERE t.ID = @Counter
IF @ItemReview = 'Tomatoes'
BEGIN
Do something here….
END
SET @Counter = @Counter + 1
END