zoukankan      html  css  js  c++  java
  • 使用游标 循环把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

  • 相关阅读:
    算法基础~链表~求两个链表的交点(不考虑时间、空间复杂度)
    《人月神话》阅读笔记一
    html与css学习笔记
    新手教程
    课程信息管理
    关于文件动手动脑
    四则运算随机生成
    关于异常
    第四次动手动脑
    第三次动手动脑
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231634.html
Copyright © 2011-2022 走看看