实现的目的是:想让更了进仓数据后马上对库存数据进行修改
create trigger tru_进仓
on 进仓
for  update
as
begin
update 结存set 现有库存=现有库存+ inserted.入仓数from inserted where 结存.代码=inserted.物料代码
if @@rowcount=0
insert 结存(代码,名称规格色别,现有库存) select 物料代码,名称规格色别,入仓数from inserted
end在SQL 2005中运行、保存都能通过,但是加了这个触发器后,要想再修改“进仓”表 任何列的数据 都会报错
是不是因为进仓表或者出仓表中有同一代码的物料出入库多次,才造成了:不能修改表的数据提示已更新或删除的行要么不能使该行成为唯一行,要么改变了多个行(2行),  这是什么原因???我觉得是不是update触发器不能使用内存中inserted临时表吗??  要怎么改??谢谢!

解决方案 »

  1.   

    如果前台不能保证数据是逐行提交,就在触发器里加循环
    create trigger tru_进仓
    on 进仓
    for update
    as
    begin
    declare @物料代码 varchar(50),@名称规格色别 varchar(50),@入仓数 int
    declare cur_i cursor for select  物料代码,名称规格色别,入仓数 from inserted
    open cur_i
    fetch next from cur_i into @物料代码,@名称规格色别,@入仓数
    while @@fetch_status=0
    begin
        update 结存 set 现有库存=现有库存+ @入仓数 where 结存.代码=@物料代码
        if @@rowcount=0
        insert 结存(代码,名称规格色别,现有库存) values( @物料代码,@名称规格色别,@入仓数)
        fetch next from cur_i into @物料代码,@名称规格色别,@入仓数
    end
    close cur_i
    deallocate cur_i
    end
      

  2.   

    create trigger tru_进仓 on 进仓 for update
    as
    begin
      if exists (select 1 from 结存 where 代码 = (select 物料代码 from inserted))
         update 结存 set 现有库存 = 现有库存 + inserted.入仓数 from 结存 , inserted where 结存.代码 = inserted.物料代码
      else
         insert 结存(代码,名称规格色别,现有库存) select 物料代码,名称规格色别,入仓数 from inserted
    end
      

  3.   

    是将修改后的数量,即inserted表里的数量加到库存数量啊,难道你是把inserted和updated的差值加上去了吗?
      

  4.   

    当然是将是把inserted和updated的差值加上去了。  那否则UPDATE起什么作用??我的仓库数据表,有可能会输入错误,要修改原来输入的数量,怎么办??