zoukankan      html  css  js  c++  java
  • sqlserver 游标的使用

    declare @temp_temp uniqueidentifier--临时变量    
    DECLARE aaa CURSOR for select Id from A
    -------------------打开游标
    open aaa
    --先查询一次再循环,防止有多个游标时@@FETCH_STATUS=-1不能进入下个游标循环的情况
    fetch next from aaa into @temp_temp
    -------------------循环取数据
    while @@FETCH_STATUS=0
    begin
    print @temp_temp
    fetch next from aaa into @temp_temp
    
    end
    ----------------------------------- 关闭游标    
    Close aaa    
    ----------------------------------- 删除游标    
    Deallocate aaa
    

      

    游标的嵌套

    declare @temp_temp uniqueidentifier--临时变量    
    DECLARE aaa CURSOR for select Id from A
    -------------------打开游标
    open aaa
    --先查询一次再循环,防止有多个游标时@@FETCH_STATUS=-1不能进入下个游标循环的情况
    fetch next from aaa into @temp_temp
    -------------------循环取数据
    while @@FETCH_STATUS=0
    begin
    print @temp_temp
    	--===========================游标嵌套 
    	DECLARE bbb CURSOR for select Id from B
    	-------------------打开游标
    	open bbb
    	--先查询一次再循环,防止有多个游标时@@FETCH_STATUS=-1不能进入下个游标循环的情况
    	fetch next from bbb into @temp_temp
    	-------------------循环取数据
    	while @@FETCH_STATUS=0
    	begin
    	print @temp_temp
    	fetch next from bbb into @temp_temp
    
    	end
    	----------------------------------- 关闭游标    
    	Close bbb    
    	----------------------------------- 删除游标    
    	Deallocate bbb
    	--===========================游标嵌套
    fetch next from aaa into @temp_temp
    
    end
    ----------------------------------- 关闭游标    
    Close aaa    
    ----------------------------------- 删除游标    
    Deallocate aaa
    

      

  • 相关阅读:
    ●BZOJ 2669 [cqoi2012]局部极小值
    ●HDU 6021 MG loves string
    试试数学公式~
    ●BZOJ 3622 已经没有什么好害怕的了
    ●BZOJ 2560 串珠子
    ●BZOJ 4361 isn
    ●BZOJ 2393 Cirno的完美算数教室
    ●BZOJ 1042 [HAOI2008]硬币购物
    ●BZOJ 2839 集合计数
    【LG2481】[SDOI2011]拦截导弹
  • 原文地址:https://www.cnblogs.com/shensigzs/p/5168944.html
Copyright © 2011-2022 走看看