用触发器:
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
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
或用存储过程:
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
insert into NewTable values(1,1,'other')
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')