SQL Server UPSERT equivalent

UPSERT functionality refers to either updating table rows based on some search condition, or inserting new rows if the search condition is not satisfied. This is intuitively seen from the word UPSERT - a combination of UPDATE (needed rows are present) or INSERT (needed rows are not present).

The most common way to implement UPSERT requires multiple statements. Let's say we have a simple table A_Table with just two columns - a key column (Id) and a data column (Data).

CREATE TABLE A_Table
(
    Id        INT    IDENTITY(1,1) NOT NULL,
    Data    VARCHAR(50)
    CONSTRAINT PK_A_Table PRIMARY KEY(id)
)
INSERT INTO A_Table (data)
VALUES
    ('data1')

At this point we have one row in A_Table:

The traditional UPSERT will look like this:

-- UPDATE or INSERT based on SELECT search predicate
--
DECLARE @key INT

SELECT @key = Id
FROM
    A_Table
WHERE
    -- Search predicates    --
    Data = 'data_searched'

IF (@key IS NOT NULL)
    -- Update part of the 'UPSERT'    --
    UPDATE
        A_Table
    SET
        Data = 'data_searched_updated'
ELSE
    -- INSERT part of the 'UPSERT'    --
    INSERT A_Table (Data)
    VALUES ('data_searched')

After

SELECT * FROM A_TABLE                            

We'll see two rows:

Fortunately, the UPSERT functionality can be implemented in one statement using MERGE statement. MERGE first appeared in SQL Server 2008.

Here is one-statement equivalent to the multi-statement UPSERT above.

MERGE INTO A_Table
USING 
    (SELECT 'data_searched' AS Search_Col) AS SRC
    -- Search predicates    --
    ON A_Table.Data = SRC.Search_Col
WHEN MATCHED THEN
    -- Update part of the 'UPSERT'    --
    UPDATE SET
        Data = 'data_searched_updated'
WHEN NOT MATCHED THEN
    -- INSERT part of the 'UPSERT'    --
    INSERT (Data)
    VALUES (SRC.Search_Col);    

If we run this query on existing two-row A_Table, we'll get this table (second row was matched and updated):

Benefits of the MERGE statement over separate SELECT/INSERT/UPDATE include:

  1. Faster performance. The Engine needs to parse, compile, and execute only one query instead of three (and no temporary variable to hold the key).
  2. Neater and simpler T-SQL code (after you get proficient in MERGE).
  3. No need for explicit BEGIN TRANSACTION/COMMIT. MERGE is a single statement and is executed in one implicit transaction.
  4. Greater functionality. MERGE can delete rows that are not matched by source (SRC table above). For example, we can delete row 1 from A_Table because its Data column does not match Search_Col in the SRC table. There is also a way to return inserted/deleted values using the OUTPUT clause.

转载于:https://www.cnblogs.com/cw_volcano/archive/2012/06/14/2548876.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值