存儲過程 遊標的使用
阿新 • • 發佈:2017-05-26
bject ide for ima procedure 重新 spa etc into
項目中有個需求領取的款項過期後自動釋放,此處我用了遊標來實現,上代碼
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 fromt_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 ------刪除遊標
存儲過程 遊標的使用