Home > DeveloperSection > Forums > How can I use select in a SQL Server while loop?
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    October-22-2013 3:44 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 1099  View(s)
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…!!!!



Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    October-22-2013 3:57 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick