create trigger xxxx on table2 after insert as
  update t1 set atonum=atonum-i.atonum 
    from table1 as t1 inner join inserted as i on t1.aid=i.aid
go注意这里假设 aid 的值是唯一的。

解决方案 »

  1.   

    CREATE TABLE t1( aid INT ,atotnum INT)
    CREATE TABLE t2(aid INT,num INT)INSERT INTO t1 SELECT 1001,100 UNION SELECT 1002,200CREATE TRIGGER tri_ins 
    on t2
    FOR INSERT
    ASupdate t1 set atotnum=atotnum-i.num from INSERTED i where i.aid=t1.aid
    INSERT INTO t2 values (1001,10)SELECT * from t1
    SELECT * from  t2select * from t1
    select * from  t2
      

  2.   

    create trigger xxxx on table2 after insert as
      update t1 set atonum=atonum-i.atonum 
        from table1 as t1 inner join inserted as i on t1.aid=i.aid
    go
      

  3.   

    CREATE TRIGGER 触发器名 ON 表名 
    FOR INSERT, UPDATE ---当插入或修改时触发
    AS
    declare @num int
    select num=@num from inserted
    update table1 set atotnum=atotnum-@num from inserted a,table2 b where a.aid=b.aid 
      

  4.   

    我修改一下我刚才发的
    CREATE TRIGGER 触发器名 ON 表名 
    FOR INSERT, UPDATE ---当插入或修改时触发
    AS
    declare @num int
    select @num=num from inserted
    update table1 set atotnum=atotnum-@num from inserted a,table2 b where a.aid=b.aid 上面这个决对行,不行你砍我.
      

  5.   

    楼上的代码都很精典,但我有一事不解:
    语句中都有“inserted as i”,这"inserted"作何解?
      

  6.   

    inserted这是当插入或修改时,系统自动生成一张临时表.保存着你插入或修改的那条记录.
    还有什么不明白吗?