如果pid和cartname可以唯一标志一行,try:CREATE TRIGGER cart ON shoppingcart 
instead of INSERT
AS
update shoppingcart 
set quantity=isnull(quantity,0)+I.quantity 
from shoppingcart T
join inserted I on I.pid=T.pid and I.cartname=T.cartname
go

解决方案 »

  1.   

    CREATE TRIGGER cart ON shoppingcart 
    instead of INSERT
    AS
    if exists(select 1 from shoppingcart as t1 join inserted as t2
     on t1.pid=t2.pid and t1.cartname=t2.cartname)
    begin
     update shoppingcart 
     set quantity=quantity+i.quantity
     from inserted as i join shoppingcart as tt
     on i.pid=tt.pid and i.cartname=tt.cartname
    end
    else
    begin
    insert into shoppingcart(pid,cartname,quantity)
    select pid,cartname,quantity from inserted
    end
    go
      

  2.   

    CREATE TRIGGER cart ON shoppingcart 
    instead of INSERT
    AS
     update shoppingcart 
     set quantity=quantity+i.quantity
     from inserted as i join shoppingcart as tt
     on i.pid=tt.pid and i.cartname=tt.cartname --没有处理pid或cartname为null的情况
     insert into shoppingcart(pid,cartname,quantity)
     select i.pid,i.cartname,i.quantity 
     from inserted as i join shoppingcart as tt
     where i.pid<>tt.pid or i.cartname<>tt.cartname
    go