使用游标 ----循环把SQL语句中的每条记录插入到另一个表中
2011-05-06 15:26
405 查看
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
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
相关文章推荐
- 使用游标循环进行SQL更新插入的SQL语句
- 利用游标循环进行更新插入的SQL事务语句
- 利用游标循环进行更新插入的SQL事务语句
- 使用SQL语句逐条更新每条记录
- 在Delphi中使用SQL语句插入包括时间日期的记录
- 使用SQL游标进行循环插入数据
- android Sqlite expert中使用SQL 语句插入两条记录
- 利用游标循环进行更新插入的SQL事务语句
- 利用游标循环进行更新插入的SQL事务语句
- 利用游标循环进行更新插入的SQL事务语句
- 如何在游标for循环中使用动态SQL语句?
- 利用游标循环进行更新插入的SQL事务语句 ~
- 利用游标循环进行更新插入的SQL事务语句
- 如何在游标for循环中使用动态SQL语句?
- SQL游标 循环处理插入、修改语句
- 一条sql语句循环插入N条不同记录
- Oracle中sql语句循环插入
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- SQL循环插入记录
- SQLite一条SQL语句插入多条记录,批量插入