用触发器:
create trigger triggername on newtable instead of insert as
insert into newtable
select  a.*
from inserted a left join newtable b
on a.fid = b.fid and a.sid = b.sid
where b.sid is null

解决方案 »

  1.   


    或用存储过程:
    CREATE PROCEDURE db.newtable_insert (@fid int;@sid int;@value varchar(30))   AS 
        if (select count(*) from newtable where fid=@fid and sid=@sid )=0 
       begin tran  
        insert into newtable values(@fid,@sid,@value)
       if @@error=0 
          commit tran
           else
          rollback tran
     return
      

  2.   

    if not exists (select * from NewTable where FID=1 and SID=1)
       insert into NewTable values(1,1,'other') 
     
      

  3.   

    declare @rowcount int
    select count(*) from newtable where fid=1 and sid=1
    select @rowcount=count(*)
    if @rowcount=0
    return
    else 
    insert into newtable values(1,1,'other')
      

  4.   

    To: tj_dns(愉快的登山者) 请问“if not exists”这些关键字及其用法是不是在SQL语言中是通用的?(即像“insert”关键字一样在所有的数据库中都是通用的写法)如果只是在MSSQL中有这些关键字,而其他数据库如 oracle或db2或mysql中没有这些用法,那就不能用这种形式了!