你的代码要改为:CREATE TRIGGER 名3 ON 子表
FOR delete
AS
delete 其他表 where 编号 in (select 编号 from deleted)
go就是把你的=改为 in

解决方案 »

  1.   

    deleted中存放的是你这个删除语句所删除的所有行!!你应该修改你的Trigger,让它能对删除的每一行进行计算!在Trigger中使用变量 @@rowcount可以知道一共有所少行数据在deleted表中!
      

  2.   

    删除一条记录就触发一次
    所以说deleted只放一条记录我觉得你如果想实现统计多条信息
    不应该用触发器
      

  3.   

    你的代码有问题。问题应该出在使用逻辑表DELETED上了。你好好看看。删除多条的时候,你不能使用等号的。要使用表和表之间的操作关系。因为这个时候不是一条数据。我想你可能是从DELETED表中读出一个值,然后进行计算了。可是它里有很多。所以会有错误出现。
      

  4.   

    删除主表的记录时,对应子表数据删除若干条,deleted表就是你删除“的若干条”
      

  5.   

    各位,触发器代码如下:
    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
      

  6.   

    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