各位,触发器代码如下: CREATE TRIGGER tg_delect ON dbo.DD_instore_d --删除入库记录的触发器 FOR delete AS declare @id int --当前的id declare @mid int --对应入库表的主表id declare @gid int--库存表中(物品小类表)的物品id declare @fnext_pc money --库存表中(物品小类表)的对上综合单价 declare @fnowcout int --库存表中(物品小类表)的现存数量 declare @fdgid int --当前的物品id declare @fincout int--入仓数量 declare @finpric money--入仓单价 declare @fnow_pc smallmoney--综合单价 declare @fupdater varchar(20)--验收人,库存最后更新人--查找删除的物品对应数据(id,对应主表的id,物品id,入仓数量,入仓单价) select @id=fdid,@mid=fmid,@fdgid=fdgid,@fincout=fincout,@finpric=finpric from deleted --查找对应主表的验收人 select @fupdater=fchecker from dd_instore_m where fmid=@mid--查找添加物品的对应数据(id,现存数量,综合单价) select @gid=fdgid,@fnowcout=fnowcout,@fnext_pc=fnow_pc from dd_goods_d where fdgid=@fdgid--删除后的综合单价的计算=[总价=(对上的综合单价× 未删除的库存数)-本次删除物品的总价(删除的入仓单价×删除的入仓数量)] ÷剩余的总个数(库存数量-入仓数量) if (@fnowcout - @fincout )>0 set @fnow_pc=(@fnext_pc*@fnowcout - @fincout*@finpric)/(@fnowcout - @fincout ) else set @fnow_pc=0--更新入库表总价,综合单价 --update dd_instore_d set ftotalp=@fincout*@finpric,fnow_pc=@fnow_pc where fdid=@id--更新库存表现存数量与综合单价 update DD_goods_d set fnowcout=@fnowcout - @fincout,fnow_pc=@fnow_pc,fupdater=@fupdater,flastdate=getdate(),flasttype='删除入库记录' where fdgid=@fdgid
CREATE TRIGGER tg_delect ON dbo.DD_instore_d --删除入库记录的触发器 FOR delete AS declare @id int --当前的id declare @mid int --对应入库表的主表id declare @gid int--库存表中(物品小类表)的物品id declare @fnext_pc money --库存表中(物品小类表)的对上综合单价 declare @fnowcout int --库存表中(物品小类表)的现存数量 declare @fdgid int --当前的物品id declare @fincout int--入仓数量 declare @finpric money--入仓单价 declare @fnow_pc smallmoney--综合单价 declare @fupdater varchar(20)--验收人,库存最后更新人declare @tmpTbl table (tid int identity(1,1),fdid int ,fmid int,fdgid int,fincout int ,finpric money ) declare @iCount int set @iCount=1--建一个表放数据,重新定义主键,方便用来取数据 insert into @tmpTbl select fdid,fmid,fdgid,fincout,finpric from deleted --做一个循环 while(@i<=@@rowcount) begin--查找删除的物品对应数据(id,对应主表的id,物品id,入仓数量,入仓单价) select @id=fdid,@mid=fmid,@fdgid=fdgid,@fincout=fincout,@finpric=finpric from @tmpTbl where tid=@iCount -- 一条一条从表中取数据--查找对应主表的验收人 select @fupdater=fchecker from dd_instore_m where fmid=@mid--查找添加物品的对应数据(id,现存数量,综合单价) select @gid=fdgid,@fnowcout=fnowcout,@fnext_pc=fnow_pc from dd_goods_d where fdgid=@fdgid--删除后的综合单价的计算=[总价=(对上的综合单价× 未删除的库存数)-本次删除物品的总价(删除的入仓单价×删除的入仓数量)] ÷剩余的总个数(库存数量-入仓数量) if (@fnowcout - @fincout )>0 set @fnow_pc=(@fnext_pc*@fnowcout - @fincout*@finpric)/(@fnowcout - @fincout ) else set @fnow_pc=0--更新入库表总价,综合单价 --update dd_instore_d set ftotalp=@fincout*@finpric,fnow_pc=@fnow_pc where fdid=@id--更新库存表现存数量与综合单价 update DD_goods_d set fnowcout=@fnowcout - @fincout,fnow_pc=@fnow_pc,fupdater=@fupdater,flastdate=getdate(),flasttype='删除入库记录' where fdgid=@fdgidset @iCount=@iCount+1 end
所以说deleted只放一条记录我觉得你如果想实现统计多条信息
不应该用触发器
CREATE TRIGGER tg_delect ON dbo.DD_instore_d --删除入库记录的触发器
FOR delete
AS
declare @id int --当前的id
declare @mid int --对应入库表的主表id
declare @gid int--库存表中(物品小类表)的物品id
declare @fnext_pc money --库存表中(物品小类表)的对上综合单价
declare @fnowcout int --库存表中(物品小类表)的现存数量
declare @fdgid int --当前的物品id
declare @fincout int--入仓数量
declare @finpric money--入仓单价
declare @fnow_pc smallmoney--综合单价
declare @fupdater varchar(20)--验收人,库存最后更新人--查找删除的物品对应数据(id,对应主表的id,物品id,入仓数量,入仓单价)
select @id=fdid,@mid=fmid,@fdgid=fdgid,@fincout=fincout,@finpric=finpric from deleted --查找对应主表的验收人
select @fupdater=fchecker from dd_instore_m where fmid=@mid--查找添加物品的对应数据(id,现存数量,综合单价)
select @gid=fdgid,@fnowcout=fnowcout,@fnext_pc=fnow_pc from dd_goods_d where fdgid=@fdgid--删除后的综合单价的计算=[总价=(对上的综合单价× 未删除的库存数)-本次删除物品的总价(删除的入仓单价×删除的入仓数量)] ÷剩余的总个数(库存数量-入仓数量)
if (@fnowcout - @fincout )>0
set @fnow_pc=(@fnext_pc*@fnowcout - @fincout*@finpric)/(@fnowcout - @fincout )
else
set @fnow_pc=0--更新入库表总价,综合单价
--update dd_instore_d set ftotalp=@fincout*@finpric,fnow_pc=@fnow_pc where fdid=@id--更新库存表现存数量与综合单价
update DD_goods_d set fnowcout=@fnowcout - @fincout,fnow_pc=@fnow_pc,fupdater=@fupdater,flastdate=getdate(),flasttype='删除入库记录' where fdgid=@fdgid
FOR delete
AS
declare @id int --当前的id
declare @mid int --对应入库表的主表id
declare @gid int--库存表中(物品小类表)的物品id
declare @fnext_pc money --库存表中(物品小类表)的对上综合单价
declare @fnowcout int --库存表中(物品小类表)的现存数量
declare @fdgid int --当前的物品id
declare @fincout int--入仓数量
declare @finpric money--入仓单价
declare @fnow_pc smallmoney--综合单价
declare @fupdater varchar(20)--验收人,库存最后更新人declare @tmpTbl table (tid int identity(1,1),fdid int ,fmid int,fdgid int,fincout int ,finpric money )
declare @iCount int
set @iCount=1--建一个表放数据,重新定义主键,方便用来取数据
insert into @tmpTbl select fdid,fmid,fdgid,fincout,finpric from deleted
--做一个循环
while(@i<=@@rowcount)
begin--查找删除的物品对应数据(id,对应主表的id,物品id,入仓数量,入仓单价)
select @id=fdid,@mid=fmid,@fdgid=fdgid,@fincout=fincout,@finpric=finpric from @tmpTbl where tid=@iCount -- 一条一条从表中取数据--查找对应主表的验收人
select @fupdater=fchecker from dd_instore_m where fmid=@mid--查找添加物品的对应数据(id,现存数量,综合单价)
select @gid=fdgid,@fnowcout=fnowcout,@fnext_pc=fnow_pc from dd_goods_d where fdgid=@fdgid--删除后的综合单价的计算=[总价=(对上的综合单价× 未删除的库存数)-本次删除物品的总价(删除的入仓单价×删除的入仓数量)] ÷剩余的总个数(库存数量-入仓数量)
if (@fnowcout - @fincout )>0
set @fnow_pc=(@fnext_pc*@fnowcout - @fincout*@finpric)/(@fnowcout - @fincout )
else
set @fnow_pc=0--更新入库表总价,综合单价
--update dd_instore_d set ftotalp=@fincout*@finpric,fnow_pc=@fnow_pc where fdid=@id--更新库存表现存数量与综合单价
update DD_goods_d set fnowcout=@fnowcout - @fincout,fnow_pc=@fnow_pc,fupdater=@fupdater,flastdate=getdate(),flasttype='删除入库记录' where fdgid=@fdgidset @iCount=@iCount+1
end