Skip to main content
Corrected the code formatting, and made the "etc" portion a comment, to make it clear that it is not part of the query.
Source Link

The best bet is to do it with a static Insert Statement if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that

I.E. While (@I <= 4) Begin Insert Into #Temp Select case @I when 1 then @1 when 2 then @2 ect End
SET @I +=1 end

While (@I <= 4)
   Begin
         Insert Into #Temp
         Select case @I when 1 then @1 
            when 2 then @2
            -- etc
            End     
     SET @I +=1
   end

The best bet is to do it with a static Insert Statement if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that

I.E. While (@I <= 4) Begin Insert Into #Temp Select case @I when 1 then @1 when 2 then @2 ect End
SET @I +=1 end

The best bet is to do it with a static Insert Statement if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that

I.E.

While (@I <= 4)
   Begin
         Insert Into #Temp
         Select case @I when 1 then @1 
            when 2 then @2
            -- etc
            End     
     SET @I +=1
   end
Source Link

The best bet is to do it with a static Insert Statement if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that

I.E. While (@I <= 4) Begin Insert Into #Temp Select case @I when 1 then @1 when 2 then @2 ect End
SET @I +=1 end