SQL Server游标

1、游标可以做循环,速度特别的快
2、使用游标解决很多数据对比上面的问题

--解决出现前台引入出现一次,手动添加条数不让保存的错误
declare @flowstatus int,@SourceBillID int,@sourceprogramid int,@deptid int,@index int=0
declare flow cursor
for(select  main.flowstatus,sum.SourceBillID,sum.sourceprogramid,sum.DeptID from AFR_Traveldetail_Sum sum 
left join afr_travelmain main on main.billid=sum.billid where sum.BillID =@billid )
--打开游标
open flow
--读取数据
fetch next from flow into @flowstatus,@SourceBillID,@sourceprogramid,@deptid
while @@FETCH_STATUS=0
	begin
		--设置过滤条件
		if isnull( @flowstatus,0)<=0 and (ISNULL(@SourceBillID,'')='' 
			begin
				--满足,证明没有引入数据
				set @index=1
			end
		else 
			begin
				--不满足,证明里面有一条数据是从其他地方引入进来的
				--找到数据了,退出循环
				set @index=0
				break
			end	
		--读取下一条
		fetch next from flow into @flowstatus,@SourceBillID,@sourceprogramid,@deptid
	end
--证明从来没有数据,提示错误信息
if @index=1
	begin
		set @result ='错误';
		return;
	end
--关闭游标
close flow
--删除游标
deallocate flow

这只是一个示例,游标的作用不止这些,批量增删改都可以使用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值