触发器问题

--库存表CODE
CREATE TABLE [dbo].[code] (
[code] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[quantity] [numeric](18, 0) NULL 
) ON [PRIMARY]
GO
--入出货表INPUT
CREATE TABLE [dbo].[input] (
[code] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[order_no] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[quantity] [numeric](10, 0) NULL ,
) ON [PRIMARY]
GO


--库存表CODE 触发器,当修改CODE编号时,修改INPUT的编号CODE
CREATE TRIGGER [TRI_CODE] ON [dbo].[code] 
FOR UPDATE
AS
if (select code from inserted) != (select code from deleted)
begin
update input set code=(select code from inserted) where code=(select code from deleted)
end


--INPUT触发器

CREATE TRIGGER [TRI_INPUT] ON dbo.[input] 
FOR UPDATE
AS
  begin
  update code
  set code.quantity= isnull(code.quantity,0) - isnull(c.quantity,0)
  from code ,(select code,sum(quantity) as quantity from deleted group by code)c
  where code.code=c.code

  update code
  set code.quantity= isnull(code.quantity,0) + isnull(b.quantity,0)
  from code ,(select code,sum(quantity)as quantity from inserted group by code)b
  where code.code=b.code
  insert code(code,quantity) select code,sum(quantity) from inserted where code not in (select code from code) group by code
  end
当我修改INPUT编号时,自动修改CODE表编号,但我在修改CODE编号时,触发了INPUT,结果计算出CODE的QUANTITY重复,请问当我修改CODE表时,能不触发INPUT表,或INPUT表这里面的SQL诗句如何写?

如下是我输入的数据:
delete from code
delete from input
insert into input (code,order_no,quantity) values ('001','order001',10)
insert into input (code,order_no,quantity) values ('001','order002',20)
select * from code
select * from input
update code set code='0001' where code='001'
本来CODE的QUANTITY是30,结果变60了!

作者: sunfor   发布时间: 2011-06-12

貌似是你的触发器 触发了另外一个触发器

作者: maco_wang   发布时间: 2011-06-12

CREATE TRIGGER [TRI_INPUT] ON dbo.[input]  
FOR UPDATE
AS
if update(quantity)
  begin
  ......

只修改数量的时候才触发

作者: FlySQL   发布时间: 2011-06-12

引用 1 楼 maco_wang 的回复:
貌似是你的触发器 触发了另外一个触发器

是啊,这两表互相触发!,当我触发了CODE表,INPUT这表能不能不触发?

作者: sunfor   发布时间: 2011-06-12

只触发一次 在INPUT触发器上加限制条件啊 ,满足什么条件的时候才触发,这个楼主可以自己来定

作者: fredrickhu   发布时间: 2011-06-12

SQL code

CREATE TRIGGER [TRI_INPUT] ON dbo.[input]  
FOR UPDATE
AS
  begin
    if update(quantity)    ---修改数量的时候才执行
    begin
  update code
  set code.quantity= isnull(code.quantity,0) - isnull(c.quantity,0)
  from code ,(select code,sum(quantity) as quantity from deleted group by code)c
  where code.code=c.code

  update code
  set code.quantity= isnull(code.quantity,0) + isnull(b.quantity,0)
  from code ,(select code,sum(quantity)as quantity from inserted group by code)b
  where code.code=b.code
  insert code(code,quantity) select code,sum(quantity) from inserted where code not in (select code from code) group by code
   end
  end


作者: X_0   发布时间: 2011-06-12

引用 5 楼 x_0 的回复:
SQL code

CREATE TRIGGER [TRI_INPUT] ON dbo.[input]
FOR UPDATE
AS
begin
if update(quantity) ---修改数量的时候才执行
begin
update code
set code.quantity= isnull(code.quantity,0) - isn……

用这个也不成啊!
因为如果INPUT表的CODE修改了,CODE表要相应变化的。

作者: sunfor   发布时间: 2011-06-12

引用 2 楼 flysql 的回复:
CREATE TRIGGER [TRI_INPUT] ON dbo.[input]
FOR UPDATE
AS
if update(quantity)
begin
......

只修改数量的时候才触发

不知您有没有相应的触发器?
我现在需要在入仓表和出仓表上分别加上触发器。

1.若入仓表新增数据,仓存表有此产品,则在上面累加;若没有则新增一条记录。
2.若入仓表修改数据,则直接跟新仓存表
3.若入仓表删除数据,(这里我还不知道能不能让用户删除??希望有经验的朋友们提示)

4.若出仓表新增,修改,则自动更新仓存表的数据。


作者: sunfor   发布时间: 2011-06-12

再看多了一眼,楼主的触发器问题还是蛮多的,就这一句
if (select code from inserted) != (select code from deleted)
来讲,如果同时更新多条记录时就会出现问题

再者,主键一般是唯一的,建议不要随便去修改它,如果只修改数量,就不会存在联动触发的情况

具体的触发器的应用,可以参考以下精华帖
http://topic.csdn.net/u/20081005/11/57061a18-c234-40ee-ba4b-1f4c3bc7f09a.html?93834

作者: FlySQL   发布时间: 2011-06-12