SQL Server - How to insert a record and make sure it is unique

前端 未结 7 1481
时光说笑
时光说笑 2020-12-08 02:45

I\'m trying to figure out the best way to insert a record into a single table but only if the item doesn\'t already exist. The KEY in this case is an NVARCHAR(400) field. Fo

7条回答
  •  自闭症患者
    2020-12-08 03:33

    declare @Error int
    
    begin transaction
      INSERT INTO Words (Word) values(@word)
      set @Error = @@ERROR
      if @Error <> 0 --if error is raised
      begin
          goto LogError
      end
    commit transaction
    goto ProcEnd
    
    LogError:
    rollback transaction
    

提交回复
热议问题