Solutions for INSERT OR UPDATE on SQL Server

后端 未结 22 1867
别跟我提以往
别跟我提以往 2020-11-21 22:23

Assume a table structure of MyTable(KEY, datafield1, datafield2...).

Often I want to either update an existing record, or insert a new record if it does

相关标签:
22条回答
  • 2020-11-21 23:02

    If going the UPDATE if-no-rows-updated then INSERT route, consider doing the INSERT first to prevent a race condition (assuming no intervening DELETE)

    INSERT INTO MyTable (Key, FieldA)
       SELECT @Key, @FieldA
       WHERE NOT EXISTS
       (
           SELECT *
           FROM  MyTable
           WHERE Key = @Key
       )
    IF @@ROWCOUNT = 0
    BEGIN
       UPDATE MyTable
       SET FieldA=@FieldA
       WHERE Key=@Key
       IF @@ROWCOUNT = 0
       ... record was deleted, consider looping to re-run the INSERT, or RAISERROR ...
    END
    

    Apart from avoiding a race condition, if in most cases the record will already exist then this will cause the INSERT to fail, wasting CPU.

    Using MERGE probably preferable for SQL2008 onwards.

    0 讨论(0)
  • 2020-11-21 23:06

    Doing an if exists ... else ... involves doing two requests minimum (one to check, one to take action). The following approach requires only one where the record exists, two if an insert is required:

    DECLARE @RowExists bit
    SET @RowExists = 0
    UPDATE MyTable SET DataField1 = 'xxx', @RowExists = 1 WHERE Key = 123
    IF @RowExists = 0
      INSERT INTO MyTable (Key, DataField1) VALUES (123, 'xxx')
    
    0 讨论(0)
  • 2020-11-21 23:08

    If you want to UPSERT more than one record at a time you can use the ANSI SQL:2003 DML statement MERGE.

    MERGE INTO table_name WITH (HOLDLOCK) USING table_name ON (condition)
    WHEN MATCHED THEN UPDATE SET column1 = value1 [, column2 = value2 ...]
    WHEN NOT MATCHED THEN INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...])
    

    Check out Mimicking MERGE Statement in SQL Server 2005.

    0 讨论(0)
  • 2020-11-21 23:08

    Do a select, if you get a result, update it, if not, create it.

    0 讨论(0)
提交回复
热议问题