SQL使用触发器避免记录重复插入

  drop   table   bb   
  
create   table     bb   (id   int,name   varchar(8))   
    
  
---添加记录   
  insert   bb   select   1,'a'   
  
union   all   select   2,'b'   
  
union   all   select   3,'c'   
  
union   all   select   4,'d'   
    
  
--创建触发器   
  create   trigger   tr1     
  
on   bb   
  
for     insert     
  
as     
  
begin   tran     
  
if   exists(select   id   from   bb   group   by   id   having   count(id)=2   )   
  
begin     
  
rollback   tran     
  
end   
  
else     
  
begin     
  
commit   tran     
  
end   
    
    
    
  
--测试     
    
  
insert   into   bb     values   (5,'d')
原文地址:https://www.cnblogs.com/hyd309/p/1328938.html