编辑:我把表结构和真正的MERGE
我正在尝试在Microsoft SQLServer 2014中使用MERGE,但我在目标表中遇到语法错误:
MERGE VALORATION_DETAIL_INPUTS_LIMIT AS Target
USING (VALUES (922, 4098)) AS Source(idValoration, idDetValInput)
ON (Source.idValoration = Target.idValoration AND Source.idDetValInput = Target.idDetValInput)
WHEN MATCHED THEN
UPDATE SET idSubject = 1633, idGood = 1114, idWarranty = 7519, idSubWarranty = 7520, units = 1.000000,
unitPrice = 250.000000, limit = 250.000000, percTax = 21.000000, tax = 52.500000, subtotal = 197.500000,
total = 250.000000
WHEN NOT MATCHED BY TARGET THEN
INSERT (idDetValInput, idValoration, idSubject, idGood, idWarranty, idSubWarranty, units,
unitPrice, limit, percTax, tax, subtotal, total)
VALUES(4093, 922, 1633, 1114, 7519, 7520, 1.000000, 250.000000, 250.000000, 21.000000, 52.500000, 197.500000, 250.000000)
OUTPUT $ACTION
错误是:
Msg 156, Level 15, State 1, Line ...
Incorrect syntax near the word 'AS'.
表格结构:
CREATE TABLE VALORATION_DETAIL_INPUTS_LIMIT
(
idDetValInput bigint NOT NULL,
idValoration bigint NOT NULL,
idSubject bigint NOT NULL,
idGood int NULL,
idWarranty int NULL,
idSubWarranty int NULL,
units real NULL,
unitPrice money NULL,
limit money NULL,
percTax real NULL,
tax money NULL,
subtotal money NULL,
total money NULL
)
答案 0 :(得分:2)
使用values子句而不是通过SELECT创建Source表,或者您也可以使用user @ m.benslimane建议的选项。
此外,由于您正在更新Target,因此无需在INSERT语句中使用Target.id,Target.value。
这很可能是POC代码,因为您使用静态值进行更新。在实际查询中要注意这一点。
MERGE VALORATION_DETAIL_INPUTS_LIMIT AS Target
USING (VALUES (922, 4098)) AS Source(idValoration, idDetValInput)
ON (Source.idValoration = Target.idValoration
AND Source.idDetValInput = Target.idDetValInput)
WHEN MATCHED THEN
UPDATE SET idSubject = 1633, idGood = 1114, idWarranty = 7519,
idSubWarranty = 7520, units = 1.000000,
unitPrice = 250.000000, limit = 250.000000,
percTax = 21.000000, tax = 52.500000, subtotal = 197.500000,
total = 250.000000
WHEN NOT MATCHED BY TARGET THEN
INSERT (idDetValInput, idValoration, idSubject, idGood,
idWarranty, idSubWarranty, units,unitPrice,
limit, percTax, tax, subtotal, total)
VALUES(1633, 1114, 7519, 7520, 1.000000,
250.000000, 250.000000, 21.000000,
52.500000, 197.500000, 250.000000,
--DUMMY VALUES I ADDED BELOW
20.00, 20.00)
OUTPUT $ACTION;
答案 1 :(得分:1)
你需要在源之后添加(id,value)。
像这样:.... Target
USING (SELECT 922 AS id, 4098 AS value) AS Source (id,value)
ON ........