将 MERGE INTO 与 Scope_IDENTITY 结合使用

发布于 2024-11-09 19:18:56 字数 1037 浏览 0 评论 0原文

Merge into 使用以下语句执行 insert 时,Scope_Identity 返回正确的代理键信息。但是,当执行更新时,Scope_Identity@@Identity 都会返回下一个可用代理键。当我添加 output 时,我在 updateinsert 上都得到了 null。

如何在更新插入上返回代理键?

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output CurrentSet.*, @Surrogate_KEY ;

 print @Surrogate_KEY
 print @@IDENTITY
 print SCOPE_IDENTITY() 

When Merge into does an insert with the following statement, Scope_Identity returns the correct surrogate key information. However when an update is performed both Scope_Identity and @@Identity return the next available surrogate key. And when I added the output, I get a null on both update and insert.

How do I return the surrogate key on both the update and the insert?

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output CurrentSet.*, @Surrogate_KEY ;

 print @Surrogate_KEY
 print @@IDENTITY
 print SCOPE_IDENTITY() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-11-16 19:18:56

OUTPUT 子句中使用插入伪表:

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output inserted.* ;

这会返回任何内容这些值位于语句末尾的表(针对受影响的行)中。

Use the inserted pseudo table in your OUTPUT clause:

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output inserted.* ;

This returns whatever the values are in the table (for the affected rows) at the end of the statement.

虫児飞 2024-11-16 19:18:56
DECLARE @Id ...
--
MERGE
    dbo.Table AS Tgt
USING
    (
        SELECT
            <Keys>
    ) AS Src
    ON Src.<Keys> = Tgt.<Keys>
WHEN MATCHED THEN
    UPDATE SET
        <...>
        ,@Id = Tgt.Id
WHEN NOT MATCHED THEN
    INSERT
    (
        ...
    )
    VALUES
    (
        ...
    )
;--
RETURN/SET/PRINT ISNULL(@Id, SCOPE_IDENTITY())
DECLARE @Id ...
--
MERGE
    dbo.Table AS Tgt
USING
    (
        SELECT
            <Keys>
    ) AS Src
    ON Src.<Keys> = Tgt.<Keys>
WHEN MATCHED THEN
    UPDATE SET
        <...>
        ,@Id = Tgt.Id
WHEN NOT MATCHED THEN
    INSERT
    (
        ...
    )
    VALUES
    (
        ...
    )
;--
RETURN/SET/PRINT ISNULL(@Id, SCOPE_IDENTITY())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文