如果一定要用触发器, 则可以这样写:create trigger tr_check on t2 for insert,update as if exists( select id1,f1 from t2 where id1 in ( select id1 from inserted union all select id1 from deleted ) group by id1,f1 having count(*)>1 ) begin raiserror('更新或者插入操作导致数据不唯一, 本次操作被取消', 16,1) rollback tran end
for insert,update
as
if exists(
select id1,f1 from t2
where id1 in (
select id1 from inserted
union all
select id1 from deleted
)
group by id1,f1
having count(*)>1
)
begin
raiserror('更新或者插入操作导致数据不唯一, 本次操作被取消', 16,1)
rollback tran
end