首先来说, 这个问题不需要触发器, 因为建立一个唯一约束就可以了添加唯一约束的代码如下alter table t2 add unique (id1,f1)

解决方案 »

  1.   

    如果一定要用触发器, 则可以这样写: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