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

  • 相关阅读:
    Unix/Linux笔记全集
    深入浅出-变长参数
    基于 SSH 的远程操作以及安全,快捷的数据传输<转>
    面向对象的特性—— 封装
    wpf 窗体翻页效果
    wpf控件拖动
    Wpf 导出CSV文件
    wpf 导出Excel
    Wpf Button 样式
    wpf简单进度条
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231634.html
Copyright © 2011-2022 走看看