2017-10-06 123 views
0

编辑:我把表结构和真正的MERGESQL服务器 - 错误MERGE

我想使用微软的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

你没有 “14号线” 在查询中。 –

+0

那么,我有一个“使用”和一个“GO”之前。我认为这不是必须的。 –

+0

现在,你的问题是不同的。 “INSERT语句中的列数多于VALUES子句中指定的值,VALUES子句中的值数量必须与INSERT语句中指定的列数相匹配。”所以,我所做的是在INSERT语句的VALUES部分末尾添加两个值,并且它工作正常。简而言之,在INSERT语句中指定的13列超过11个在INSERT语句的VALUES部分中提供的VALUES。还有2个需要提供的值。 – Amit

回答

1

您需要在作为源代码后添加(id,value)。

这样的:

.... Target 
USING (SELECT 922 AS id, 4098 AS value) AS Source (id,value) 
ON ........ 
2
  1. 使用价值条款,而不是通过SELECT创建源表,或者您也可以使用用户@ m.benslimane建议选项。

  2. 此外,由于您正在更新Target,因此您无需在INSERT语句中使用Target.id,Target.value。

  3. 这是最有可能的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; 
    
+0

我修复了这些东西,谢谢你的提示。但是我仍然得到了同样的错误。看起来像SQL Server不喜欢MERGE,:( –

+0

@AndrésMarotta我创建了一个表并运行在我看到它正常工作后,我查询到的查询可以在查询中粘贴你的表结构吗? – Amit

+0

另外,你正在使用哪个版本的SQL服务器? – Amit