SQL-游标
作者:互联网
游标使用实例:
ALTER trigger [dbo].[ICStockBill_IU_YSKF] on [dbo].[ICStockBill]
FOR UPDATE
AS
SET NOCOUNT ON
--出库单审核时触发
IF EXISTS (select 1 from inserted where FTranType=29 and isnull(FCheckerID,0)<>0)
BEGIN
--拿到inserted中的物料内码和数量(出库单)
declare @deptId int,@num int,@batch varchar(50),@price decimal(18,2),@yyze_je decimal(18,2),@ys_je decimal(18,2),
@yyje decimal(18,2),@fitemId int,@finterId int,@yyzeall decimal(18,2)=0
DECLARE cursor_name CURSOR FOR --定义游标
select a.FDeptID,b.FAuxQty,b.FBatchNo,c.YSFAmount,b.FItemID,b.FInterID
from inserted a
inner join ICStockBillEntry b on a.FInterID=b.FInterID
inner join YZ_YSKF c on a.FDeptID=c.DeptId
inner join t_ICItem d on b.FItemID=d.FItemID
where a.FTranType=29
OPEN cursor_name --打开游标
FETCH NEXT FROM cursor_name INTO @deptId,@num,@batch,@ys_je,@fitemId,@finterId --into的变量数量必须与表的列数量相等,此处等于赋值
WHILE @@FETCH_STATUS = 0 --进入循环
BEGIN
--拿入库单中的单价
select @price=b.FPrice
from ICStockBill a
inner join ICStockBillEntry b on a.FInterID=b.FInterID
where a.FTranType=10 and a.FDeptID=@deptId
and b.FBatchNo=@batch and isnull(a.FCheckerID,0)<>0 --查找对应入库单单价(物料内码和批号)
and b.FItemID=@fitemId
--已用总金额
set @yyje=@price * @num
select @yyze_je= @yyje + isnull(YYED,0)
from YZ_YSKF where DeptId=@deptId
set @yyzeall+=@yyze_je
FETCH NEXT FROM cursor_name INTO @deptId,@num,@batch,@ys_je,@fitemId,@finterId --循环
END
CLOSE cursor_name --关闭游标
DEALLOCATE cursor_name --释放游标
--declare @o varchar(50)
--set @o=CAST(@yyzeall as varchar(50))
--RAISERROR(@o,18,18)
IF(@yyzeall>@ys_je)
BEGIN
declare @ce varchar(50),@pr varchar(50)
ROLLBACK TRANSACTION --数据回滚
set @ce=cast(@yyzeall-@ys_je as varchar(50))
set @pr=cast(@price as varchar(50))
declare @ret varchar(50)
set @ret='超出预算'+@ce
RAISERROR(@ret,18,18) --弹窗提示
END
END
标签:varchar,--,18,50,游标,SQL,je 来源: https://www.cnblogs.com/-GoFurther/p/14268100.html