Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help with the Query fast


i want to perform this 'for loop' in stored procedure

for (int i=0;i<4;i++)
i want to copy table data to another table using for loop in stored procedure
By using #temp
Posted
Updated 1-Feb-16 20:42pm
v2
Comments
dan!sh 2-Feb-16 1:49am    
You do not need to write loop. There are other ways. Search the web.

You do not need a loop to copy a table.

If its a new table, you can use Select * Into - SQL SELECT INTO Statement[^].

For an existing table, try something like insert into ABC_1 select * from ABC
 
Share this answer
 
Comments
Member 12301708 2-Feb-16 1:57am    
i want to copy data row by row
As other said you dont need to have a loop for such insertions, but anyway in complex scenarios we may require this kind of approach. this should get you going. sql - Inserting a multiple records in a table with while loop - Stack Overflow[^]
 
Share this answer
 
User Cursor for your scenario :

DECLARE Select_ CURSOR FOR
select Col1,Col2 from ABC

OPEN Select_
declare @Col1 varchar(15),@Col2 varchar(15)

FETCH NEXT FROM Select_ into @Col1 ,@Col2
WHILE @@FETCH_STATUS = 0
BEGIN
insert into XYZ (Col1,Col2)
FETCH NEXT FROM Select_ into @Col1 ,@Col2
END

CLOSE Select_
DEALLOCATE Select_
 
Share this answer
 
Comments
Member 12301708 4-Feb-16 1:14am    
Thank you dushi0607

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900