SQL Server 2008: INSERT if not exits, maintain unique column

前端 未结 1 706
萌比男神i
萌比男神i 2020-12-06 23:47

I\'m using SQL Server 2008.

I\'ve got a column NVARCHAR(MAX) in a table which I want to make sure is unique. The table has 600,000 records and grows ev

相关标签:
1条回答
  • 2020-12-07 00:06

    It's madness not to have an index.

    It would help but the index key length can only be 900 bytes.

    However, it's likely you already have duplicates because the potential for a 2nd EXISTS to run after the 1st EXISTS but before the 1st INSERT.

    The index creation will tell you, and subsequently protect against this.

    However, you can get errors under heavy load.

    My favoured approach for high inserts/low duplicates is the JFDI pattern. Highly concurrent

    BEGIN TRY
       INSERT etc
    END TRY
    BEGIN CATCH
        IF ERROR_NUMBER() <> 2627
          RAISERROR etc
    END CATCH
    
    0 讨论(0)
提交回复
热议问题