Skip to content
Advertisement

SQL While Loop is Is Printing same ID for Every Record

My resultset has 2958 rows. I’m trying to Loop over the result set and select each row, assigning a new id to it, but each new Id is printing 2958 times. What am I doing wrong and how could I fix this? After the select is correct, I want to use it with the Insert.

Advertisement

Answer

The main problem in your loop, I think, is that you’re just selecting all the rows instead of specifying which row you want for this iteration of the loop.

But you don’t need a loop for this at all. You’re building a loop based on the count of a specific set of rows in the source table; just use those same rows to generate a set.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement