这个如果不用触发器直接入库也是可以的,但是当表中数据很多时,执行查询表中sname是否已经存在时就变的很慢了,而且入库的流量也很大 ,是不是要加上事务呢,我也不知道事务该怎么加,请大家帮帮忙呀

解决方案 »

  1.   

    create table testa (id int identity primary key,sname varchar(8),stime varchar(8))
    go 
    create table testb (id int identity primary key,sname varchar(8),click int)
    go 
    create table testc (id int identity primary key,sname varchar(8),stime varchar(8))
    go
    按照要求创建触发器:create trigger tt
    on testa 
    for insert 
    as 
    insert testc select sname,stime from inserted
    if exists (select b.sname from testb b join inserted s  on b.sname=s.sname )
    begin
    update testb set click=click+1 from testb b join inserted s  on b.sname=s.sname
    end
    else
    begin
    insert testb select (select sname from inserted),1
    end --------
      

  2.   

    create trigger tt
    on testa 
    for insert 
    as insert testc (sname,stime) select sname,stime from inserted --插入testc--处理testb
    if exists (select * from testb b, inserted i where b.sname=i.sname )
         update b set b.click=b.click+1 from testb b,inserted i  where b.sname=i.sname
    else
         insert testb(sname,stime ) select sname,1 from inserted
      

  3.   

    Create trigger trdtest
        On testa
        instead of insert
    As
    update testb set click=click+1 from inserted a,testb b where a.sname=b.sname   insert into testb(sname,click)
          select sname,1 from inserted A
    WHERE NOT EXISTS(SELECT 1 FROM TESTB WHERE SNAME=A.SNAME)   
       insert into testc(sname,stime)
          select sname,stime from insertedgo