我想在存储过程中使用for循环将表数据复制到另一个表 [英] i want to copy table data to another table using for loop in store procedure
本文介绍了我想在存储过程中使用for循环将表数据复制到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
快速查询帮助
i希望在存储过程中执行'for loop'
for(int i = 0; i< 4; i ++)
i希望使用存储过程中的for循环将表数据复制到另一个表
使用#temp
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
推荐答案
您不需要循环来复制表格。
如果是新表,您可以使用选择*进入
- SQL SELECT INTO声明 [ ^ ]。
对于现有的表,尝试类似插入ABC_1 select * from ABC
You do not need a loop to copy a table.
If its a new table, you can useSelect * Into
- SQL SELECT INTO Statement[^].
For an existing table, try something likeinsert into ABC_1 select * from ABC
正如其他人所说,你不需要为这种插入设置循环,但无论如何在复杂场景中我们可能需要这种方法。这应该让你去。 sql - 使用while循环在表中插入多个记录 - 堆栈溢出 [ ^ ]
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[^]
您的场景的用户光标:
DECLARE Select_ CURSOR FOR
从ABC选择Col1,Col2
OPEN Select_
声明@ Col1 varchar(15),@ Col2 varchar(15)
FETCH NEXT FROM Select_ into @ Col1,@ Col2
WHILE @@ FETCH_STATUS = 0
BEGIN
插入XYZ(Col1,Col2)
FETCH NEXT FROM Select_ into @ Col1,@ Col2
END
CLOSE Select_
DEALLOCATE选择_
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_
这篇关于我想在存储过程中使用for循环将表数据复制到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文