当Merge into
使用以下语句执行insert
时,Scope_Identity
会返回正确的代理键信息。但是,当执行update
时,Scope_Identity
和@@Identity
都会返回 next 可用的代理键。当我添加output
时,update
和insert
都会显示空值。
如何在update
和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()
答案 0 :(得分:10)
使用OUTPUT clause中的inserted
伪表:
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.* ;
这将返回语句末尾表中的值(对于受影响的行)。
答案 1 :(得分:4)
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())