Solutions for INSERT OR UPDATE on SQL Server

后端 未结 22 2009
别跟我提以往
别跟我提以往 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: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.

提交回复
热议问题