HOW CAN I USE SELECT IN A SQL SERVER WHILE LOOP?

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
 1407  View(s)
Ratings:
Rate this:

How can I use select in a SQL Server while loop? Below have my demo code: -

 declare @loopVal integer
 set @loopVal =1
 while (@loopVal <10)
 begin
   select @loopVal as m;
   set @loopVal =@loopVal +1
 END 

I want it to return all select results in one table but it will returns 10 separate table ... how can I achieve my task? 

Please help me out…!!!!

  1. Samuel Fernandes

    Post:159

    Points:1117
    Re: How can I use select in a SQL Server while loop?

    declare @loopVal integer
    DECLARE @testTable TABLE(
       /*Write your data type here*/
    )
     set @loopVal =1
     while (@loopVal <10)
     begin
       insert into @testTable select @loopVal;
       set @loopVal =@loopVal +1
     END 
    select * from @testTable

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!