首页 > 代码库 > 存储过程 游标的使用
存储过程 游标的使用
项目中有个需求领取的款项过期后自动释放,此处我用了游标来实现,上代码
USE [QHWCloud] GO /****** Object: StoredProcedure [dbo].[pro_Shifang] Script Date: 05/26/2017 15:04:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[pro_Shifang] as declare @id int; declare @money decimal; declare @miid int; declare @SumMoney decimal; declare @linShimoney decimal; declare @Paymoney decimal; DECLARE CUR_GuoQi CURSOR scroll For --创建游标,查询过期数据 select mr_id,mi_id,mi_receivermoney from t_money_receive t where t.mi_endtime< GETDATE() and t.mi_state=1 ; open CUR_GuoQi; --打开游标 begin fetch first from CUR_GuoQi into @id,@miid,@money --我理解为此处获取游标的第一行 While @@FETCH_STATUS=0 ---返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。 if(@id!=‘‘) begin update t_money_receive set t_money_receive.mi_state=2 where t_money_receive.mr_id=@id; --将该条申请记录状态修改为2 ,释放 set @linShimoney=(select t_money_info.mi_surplusmoney from t_money_info where t_money_info.mi_id=@miid); --查询可用余额 set @SumMoney=@linShimoney+@money; --重新计算可用余额 set @Paymoney=(select pay_money from t_money_info where t_money_info.mi_id=@miid) --查询款项的支付金额 if(@Paymoney=@SumMoney) --释放后没有其他的领取申请 begin update t_money_info set t_money_info.mi_surplusmoney=@SumMoney , t_money_info.mi_state=1 where t_money_info.mi_id=@miid; --将领取金额返回到款项可用金额中,并且将状态修改为未领取 end else update t_money_info set t_money_info.mi_surplusmoney=@SumMoney, t_money_info.mi_state=2 where t_money_info.mi_id=@miid; --将领取金额返回到款项可用金额中,并且将状态修改为部分领 fetch next from CUR_GuoQi into @id,@miid,@money -----提前下一条 信息 end end Close CUR_GuoQi;----关闭游标 deallocate CUR_GuoQi ------删除游标
存储过程 游标的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。