gpt4 book ai didi

sql-server - SQL合并中的打印语句

转载 作者:行者123 更新时间:2023-12-05 07:58:01 26 4
gpt4 key购买 nike

有人要求我在 SQL 合并中包含一些调试 PRINT 语句,我是新手。我被告知 OUTPUT 不令人满意。

MERGE Production.UnitMeasure AS target
USING (SELECT @UnitMeasureCode, @Name) AS source (UnitMeasureCode, Name)
ON (target.UnitMeasureCode = source.UnitMeasureCode)
WHEN MATCHED THEN
UPDATE SET Name = source.Name
PRINT "Updated"
WHEN NOT MATCHED THEN
INSERT (UnitMeasureCode, Name)
VALUES (source.UnitMeasureCode, source.Name)
PRINT "Inserted"

这是行不通的,因为看起来每个条件 block 都应该包含一个 SQL 命令。

是否有一种简单的方法可以实现我所追求的目标?

最佳答案

IF EXISTS (SELECT 1 FROM Production.UnitMeasure  WHERE UnitMeasureCode = @UnitMeasureCode)
BEGIN
UPDATE Production.UnitMeasure
SET Name = @Name
WHERE UnitMeasureCode = @UnitMeasureCode
PRINT 'Updated'
END
ELSE
BEGIN
INSERT Production.UnitMeasure (UnitMeasureCode, Name)
VALUES (@UnitMeasureCode, @Name)
PRINT 'Inserted'
END

或者另一种方法可以是

     UPDATE Production.UnitMeasure
SET Name = @Name
WHERE UnitMeasureCode = @UnitMeasureCode

IF (@@ROWCOUNT <> 0)
BEGIN
PRINT 'Updated'
END
ELSE
BEGIN
INSERT Production.UnitMeasure (UnitMeasureCode, Name)
VALUES (@UnitMeasureCode, @Name)
PRINT 'Inserted'
END

关于sql-server - SQL合并中的打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25669250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com