感谢 NLwino 提出这个问题: Using output to set a variable in a merge statement
我正在寻找从 MERGE 内部返回我的新 counter_ 的值:
DECLARE @CounterType VARCHAR(50)
SET @CounterType = 'Case';
MERGE [dbo].[AN_Counter] WITH(ROWLOCK) TARGET
USING(SELECT @CounterType) SOURCE([KEY])
ON TARGET.[System_Name] = SOURCE.[KEY]
WHEN MATCHED THEN
UPDATE
SET COUNTER_ = COALESCE(COUNTER_,0) + 1
SELECT @CounterType = COALESCE(Prefix,'') + CAST(SUBSTRING('0000000000000000000000000000',1,padding - LEN(CAST (Counter_ AS VARCHAR ))) + CAST (Counter_ AS VARCHAR ) AS VARCHAR)
;
我想执行合并,并将新值放入 @CounterType 变量中,如上所述。
任何帮助深表感谢。米