create trigger tr_score_ins
on score
INSTEAD OF insert
as
if exists (select * from score a,inserted b where a.xh=b.xh and a.kmh=b.kmh)
begin
   ROLLBACK TRANSACTION
end

解决方案 »

  1.   

    inalover(奇遇) :
    一个数据也插不进呀
      

  2.   

    抱歉,这样用可以
    create trigger tr_score_ins
    on score
    for insert
    as
    declare @xha char(20),@kcha char(20)
    declare @cnt
    select @xha=xh,@kcha=kch from inserted
    select @cnt=count(*) from score where xh=@xha and kch=@kch
    if @cnt>1
    begin
       ROLLBACK TRANSACTION
    end
      

  3.   

    create trigger tr_score_ins
    on score
    for insert
    as
    if exists (select * from score a,inserted b where a.xh=b.xh and a.kmh=b.kmh)
    begin
       ROLLBACK TRANSACTION
    end
      

  4.   

    呵呵,确实我的也错了!
    inalover(奇遇)后面的写法再一次插入一行的情况下是对的。
    create trigger tr_score_ins
    on score
    INSTEAD OF insert
    as
    if exists (select * from score a,inserted b where a.xh=b.xh and a.kmh=b.kmh)
    begin
       ROLLBACK TRANSACTION
    end
    INSERT score
    SELECT * FROM INSERTED
    GO
      

  5.   

    或者:
    create trigger tr_score_ins
    on score
    for insert
    as
    if exists (select a.xh,a.kmh,COUNT(*) AS A from score a,inserted b where a.xh=b.xh and a.kmh=b.kmh GROUP BY a.xh,a.kmh HAVING COUNT(*)>1)
    begin
       ROLLBACK TRANSACTION
    end
      

  6.   

    谢谢各位,我马上就给分。不过,我的记录最大时有近5万条记录,用count()是不是效率太低了。
      

  7.   

    to: Yang_(扬帆破浪) 
    inalover(奇遇)后面的写法再一次插入一行的情况下是对的。
    ——————这个是不行的。这样可以插入同样的数据。