使用游标 循环把SQL语句中的每条记录插入到另一个表中

declare cur cursor
read_only
for select PName from Point 

declare @name varchar(40)
open cur

fetch next from cur into @name
while (@@fetch_status =0)
begin
print @name

fetch next from cur into @name
end

close cur
deallocate cur
go

示例

declare @id int
declare cursor1 cursor for         --定义游标cursor1
select id from Evaluation where sortpath='0,57,68,'               --使用游标的对象(跟据需要填入select文)
open cursor1                       --打开游标
fetch next from cursor1 into @id

while @@fetch_status=0           --判断是否成功获取数据
begin
insert into ResultsItems (Sortpath,ResultsItems,Score,Pid)values('0,57,68','非常满意',0,@id)
                     --进行相应处理(跟据需要填入SQL文)
fetch next from cursor1 into @id  --将游标向下移1行
end
close cursor1                   --关闭游标
deallocate cursor1

原文地址:https://www.cnblogs.com/yeye518/p/2231634.html