Fast upsert Sql server 2008 R2

前端 未结 2 567
忘掉有多难
忘掉有多难 2021-01-04 01:25

I\'m trying to upsert records using SP into one table.

CREATE TABLE [dbo].[SHARE_AD_GROUP](
    [SHARE_AD_GROUP_ID] [int] IDENTITY(1,1) NOT NULL,
    [SHARE_         


        
2条回答
  •  囚心锁ツ
    2021-01-04 01:56

    I think merge statement is faster then what you demonstrate in your 2 ways. if you need to know more about how to write MERGE INTO Query in sql server then please follow the links..

    http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/

    https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/

    and your merge block looks like this, you need to put that in your store procedure or any other places.

    MERGE INTO SHARE_AD_GROUP A
    USING (
        SELECT SHARE_AD_GROUP_ID,
            SHARE_ID,
            AD_GROUP,
            SHARE_PERMISSIONS
        FROM SHARE_AD_GROUP
        WHERE SHARE_ID = @shareID AND AD_GROUP = @ownerId
    ) B ON (A.SHARE_AD_GROUP_ID = B.SHARE_AD_GROUP_ID)
    WHEN MATCHED THEN
        UPDATE SET A.SHARE_PERMISSIONS = B.SHARE_PERMISSIONS
    WHEN NOT MATCHED THEN
        INSERT (SHARE_PERMISSIONS) VALUES(@sharePermissions);
    

提交回复
热议问题