有表A(id,hits),C(id,shop_id,content)
其中C表的shop_id是A表的外键
C表建一触发器,要在C插入一条记录的时候,更新A表对应point+1
该如何实现呢?

解决方案 »

  1.   

    我的想法就是如果能知道插入记录的shop_id,就可以了
    所以,现在的问题时如何知道插入记录的shop_id
      

  2.   

    有表A(id,hits),
    更新A表对应point+1
    ?point这是那一个表的列
      

  3.   

    如果是单线程的话,inserted中同时只有一条记录
    那么就可以使用select C.shop_id from C,inserted where C.id = inserted.id
    但是如果在多线程条件下,inserted中同时又多条记录,就不可以了
      

  4.   

    create table A(id int, hits int)
    go
    create table C(id int, shop_id int, content varchar(200))
    goinsert A select 1, 0
    union all select 2, 0
    union all select 3, 0
    union all select 4, 0
    go
    create trigger tr on C
    for insert
    as
    update A set hits=hits+1
    from inserted as C
    where A.id=C.shop_id
    goinsert C select 1, 2, 'AA'
    go
    select * from A
    go--result
    id          hits        
    ----------- ----------- 
    1           0
    2           1
    3           0
    4           0(4 row(s) affected)
      

  5.   

    会不会出现这样的情况:
    插入C表的两条记录m1,m2
    m1在m2之前插入,但是m1的速度慢;因为插入数据的过程是先把数据插入到inserted表。那么如果m1插入到inserted中,但是还没有把数据插入C表中,这时候m2把数据插入了inserted中。
    那么,现在有两个问题:
    1、inserted中现在是不是有两条数据?
    2、如果有两条数据,那么m1对应的A表的记录就会更新两次,但是我只想更新一次,这样还是不可以阿
      

  6.   

    有表A(id,hits),C(id,shop_id,content)
    其中C表的shop_id是A表的外键
    C表建一触发器,要在C插入一条记录的时候,更新A表对应point+1
    该如何实现呢?
    试试
    create trigger autoA
    on c
    for insert
    begin
    update A set hits=hits+1 from A,C where id=shop_id
    end