美文网首页
SQL Server 游标的简单使用

SQL Server 游标的简单使用

作者: sttone | 来源:发表于2020-07-13 08:12 被阅读0次
declare @id int --定义接收变量
declare @name nvarchar(20) --定义接收变量
declare type_cur cursor static  --定义游标[static:静态游标]
for 
  select id,type_name from ST_Type
open type_cur 
fetch next from type_cur into @id,@name --循环游标
while @@FETCH_STATUS =0 --假如检索到了数据继续执行
begin
    ---------------逻辑开始部分-----------------
    print @name
    if(@id=2)
    begin
        update ST_Type set type_name='测试:'+type_name where id=3
    end
    ---------------逻辑结束部分-----------------
    fetch next from type_cur into @id,@name --获取下一条数据并赋值给变量
end
close type_cur --关闭游标
deallocate type_cur --删除游标

在数据比较多的情况下使用游标会造成读取数据缓慢或者运行错误
建议在数据量比较大的时候少游标,或者分段查询,也可以使用零时表查询

相关文章

网友评论

      本文标题:SQL Server 游标的简单使用

      本文链接:https://www.haomeiwen.com/subject/qmbocktx.html