I’m trying to put the results of a SELECT into a variable and loop through the results to manipulate that data, all in the same stored proceedure… Here’s what I have so far:
DECLARE @i int
@Result = (SELECT * FROM UserImport)
SET @i = 0
WHILE @i < (SELECT Count(@Result) As Count)
BEGIN
/* Do Stuff */
END
I know I’m way off because it’s saying @Result was not declared, but I’m not sure how to declare a variable to be able to hold the results of a SELECT statement.
Can anyone tell me where i’m going wrong and how to fix it?
Thanks,
Matt
You can use a cursor, at least that’s the traditional way of doing it. You can also use a while loop as you requested. See this article for an example of a cursor and the alternative.
Avoiding SQL cursors