有兩個table.要在table2上作個update時的觸發器.當update table2的數量時,判table2的該產品sum(數量)不能和table1的該產品sum(數量)不相等.
一句話就是,兩個表的各產品總數得一樣.
table1 
(
產品名稱 char(20)
數量 int
)table2
(
產品名稱 char(20)
位置   int
數量   int
)

解决方案 »

  1.   

    create trigger tu_table2 on table2
    for update
    as
    if update(数量)
    begin
    update table1
    set 数量=i.数量
    from table1,inserted i
    where table1.产品名称=i.产品名称
    end
      

  2.   

    表中产品和数量是1比1么?
    如果是,就不用sum了吧
    ls正解~
      

  3.   

    create trigger tu_table2 on table2
    for update
    as
    if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
    begin
    update table1
    set 数量=t.数量
    from table1,(select 产品名称,sum(数量) as 数量 from table2 group by 产品名称)t,inserted i,deleted d
    where table1.产品名称=i.产品名称 and i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0) and t.产品名称=i.产品名称
    end
      

  4.   

    我的table1不修改
    就是在修改table2的數據是判斷是否會和table1總數不同.
      

  5.   

    你的意思是在修改table2的时候判断是不是和table1的总数相不相等?不相等就不能修改吗?
      

  6.   

    是的,如果修改table2時發現,用戶寫的數量總數不和table1相等,就報錯,不許改.相等才允許改
      

  7.   

    create trigger tu_table2 on table2
    for update
    as
    if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
    begin
    if exists(select 1 from (select 产品名称,sum(isnull(数量,0)) as 数量 
    from table2 group by 产品名称)t,table1,inserted i,deleted d
    where t.产品名称=table1.产品名称 
    and t.数量<>isnull(table1.数量,0) 
    and t.产品名称=i.产品名称 
    and i.产品名称=d.产品名称 
    and isnull(i.数量,0)<>isnull(d.数量,0))
    raiserror('修改的数量和table1中的总数不相等!',16,1)
    rollback tran
    return
    end
      

  8.   

    數據舉例
    table1
    產品名稱 數量
    1.產品1  50
    2.產品2  60table2
    產品名稱 位置 數量
    1.產品1  100   10
    2.產品2  100   60
    3.產品1  200   40現在要修改table2的第三條紀錄為 數量50  修改時判斷,table2的總數得和table1一樣,不一樣報錯"數據總數不對,請修改"...改對了,才可以update table2
      

  9.   

    數據舉例
    table1
    產品名稱 數量
    1.產品1  50
    2.產品2  60table2
    產品名稱 位置 數量
    1.產品1  100   10
    2.產品2  100   60
    3.產品1  200   30  -----数据改变
     
    修改后
    table2
    產品名稱 位置 數量
    1.產品1  100   10
    2.產品2  100   60
    3.產品1  200   40
    才能提交 更新 ?
      
      

  10.   


    drop table table1
    drop table table2
    create  table table1(name varchar(20),shumu int)
    insert into table1 values('產品1',50)
    insert into table1 values('產品2',60)create  table table2(id int,name varchar(20),dizhi varchar(20), shumu int)
    insert into table2 values(1,'產品1','a',10)
    insert into table2 values(2,'產品2','b',60)
    insert into table2 values(3,'產品1','b',30)select * from table1
    select * from table2create trigger tr_t2 on table2
     for update
    as
     begin if (select sum(shumu) from table2 where id<> (select id from deleted)  and name=(select name from inserted))
    +
    (select shumu from inserted ) in (select shumu from table1 where name=(select name from inserted))
    update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)
    else 
      ---不做任何事
    end
      

  11.   

    我在table2中加了个ID 字段 好区分更新的具体行数
      还没测试  待改正
      

  12.   

    试试这个drop table table1
    drop table table2
    create  table table1(name varchar(20),shumu int)
    insert into table1 values('產品1',50)
    insert into table1 values('產品2',60)create  table table2(id int,name varchar(20),dizhi varchar(20), shumu int)
    insert into table2 values(1,'產品1','a',10)
    insert into table2 values(2,'產品2','b',60)
    insert into table2 values(3,'產品1','b',30)select * from table1
    select * from table2drop trigger tr_t2create trigger tr_t2 on table2
     for update
    as
     begin if ((select sum(shumu) from table2 where id<> (select id from deleted)  and name=(select name from inserted))
    +
    (select shumu from inserted )) in (select shumu from table1 where name=(select name from inserted))
    update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)else update table2 set shumu=(select shumu from deleted) where id=(select id from deleted)end
    update table2 set shumu=20 where id=3
      

  13.   

    for update触发器是在数据更新到表里之后才触发,如果你没有rollback tran,那它就是更新成功,而不用再update自身一次.
      

  14.   

    那这样写 ?create trigger tr_t2 on table2
     for update
    as
     begin if ((select sum(shumu) from table2 where id<> (select id from deleted)  and name=(select name from inserted))
    +
    (select shumu from inserted )) in (select shumu from table1 where name=(select name from inserted))
    update table2 set shumu=(select shumu from inserted) where id=(select id from deleted)else raiserror('修改的数量和table1中的总数不相等!',16,1)
    rollback tran
    returnend   结果和在重新更新是一样的吧
      

  15.   

    onlyonereason(学习sql中........) ( ) 信誉:100    Blog   加为好友  2007-04-10 10:11:05  得分: 0  
     
     
       是啊 
      我写的是一条一条的 
     对触发器多条更新还比较陌生
      
    ---------------------------------------------------
    不是说你写的触发器是一条一条的,是说更新多条的时候会出错.
    试试这条语句会不会报错?
    update table2 set shumu=50 where id in(1,3)
      

  16.   

    没必要写触发器,
    把table1做成视图不就得了,
      

  17.   

    create view table1 
    as
    select partname,sum(quantity) as AmountQty 
    from tabel2 group by partname
      

  18.   

    gahade(与君共勉) 的觸發器有點問題,需要做如下修改。這裡的Begin End少了,會導致正確的更新也沒有辦法進行。create trigger tu_table2 on table2
    for update
    as
    if update(数量) and exists(select 1 from inserted i,deleted d where i.产品名称=d.产品名称 and isnull(i.数量,0)<>isnull(d.数量,0))
    begin
    if exists(select 1 from (select 产品名称,sum(isnull(数量,0)) as 数量 
    from table2 group by 产品名称)t,table1,inserted i,deleted d
    where t.产品名称=table1.产品名称 
    and t.数量<>isnull(table1.数量,0) 
    and t.产品名称=i.产品名称 
    and i.产品名称=d.产品名称 
    and isnull(i.数量,0)<>isnull(d.数量,0))
    Begin --這裡少了Begin
    raiserror('修改的数量和table1中的总数不相等!',16,1)
    rollback tran
    End --這裡少了End
    return
    end
    GO