如何解决这个SQL查询?我找不到解决方案

时间:2018-12-24 10:38:25

标签: sql sql-server syntax-error

我正在尝试执行此查询,但是它不起作用,并且出现错误消息。

SELECT 
    LAMACOM.GACCENTRYD.ACCNUM_0, 
    CASE
       WHEN LAMACOM.GACCENTRYD.SNS_0 < 0
            AND LAMACOM.GACCENTRY.RVS_0 <> 3
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVC"
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVE"
            AND LAMACOM.PAYMENTH.PAYTYP_0 = "CCHQT"
            AND LAMACOM.PAYMENTH.STA_0 < 5
          THEN LAMACOM.GACCENTRYD.AMTLOC_0 * 1
       WHEN LAMACOM.GACCENTRYD.SNS_0 < 0
            AND LAMACOM.GACCENTRY.RVS_0 <> 3
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVC"
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVE"
            AND LAMACOM.PAYMENTH.PAYTYP_0 = "CTRT"
            AND LAMACOM.GACCENTRY.DUDDAT_0 = GETDATE()
          THEN LAMACOM.GACCENTRYD.AMTLOC_0 * 1
    END AS ENC_NC
FROM 
    LAMACOM.GACCENTRY, LAMACOM.GACCENTRYD, LAMACOM.PAYMENTH
WHERE
    LAMACOM.GACCENTRYD.TYP_0 = LAMACOM.GACCENTRY.TYP_0 
    LAMACOM.GACCENTRYD.NUM_0 = LAMACOM.GACCENTRY.NUM_0
    LAMACOM.PAYMENTH.BPRVCR_0 = LAMACOM.GACCENTRY.NUM_0

这就是问题

  

Microsoft SQL:语法与“ LAMACOM”不符。

我无法确定问题来自哪个表。

5 个答案:

答案 0 :(得分:2)

您必须在WHERE子句中放置AND或OR条件

SELECT 
    LAMACOM.GACCENTRYD.ACCNUM_0, 
    CASE
       WHEN LAMACOM.GACCENTRYD.SNS_0 < 0
            AND LAMACOM.GACCENTRY.RVS_0 <> 3
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVC"
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVE"
            AND LAMACOM.PAYMENTH.PAYTYP_0 = "CCHQT"
            AND LAMACOM.PAYMENTH.STA_0 < 5
          THEN LAMACOM.GACCENTRYD.AMTLOC_0 * 1
       WHEN LAMACOM.GACCENTRYD.SNS_0 < 0
            AND LAMACOM.GACCENTRY.RVS_0 <> 3
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVC"
            AND LAMACOM.GACCENTRY.TYP_0 <> "AVE"
            AND LAMACOM.PAYMENTH.PAYTYP_0 = "CTRT"
            AND LAMACOM.GACCENTRY.DUDDAT_0 = GETDATE()
          THEN LAMACOM.GACCENTRYD.AMTLOC_0 * 1
    END AS ENC_NC
FROM 
    LAMACOM.GACCENTRY, LAMACOM.GACCENTRYD, LAMACOM.PAYMENTH
WHERE
    LAMACOM.GACCENTRYD.TYP_0 = LAMACOM.GACCENTRY.TYP_0 AND
    LAMACOM.GACCENTRYD.NUM_0 = LAMACOM.GACCENTRY.NUM_0 AND 
    LAMACOM.PAYMENTH.BPRVCR_0 = LAMACOM.GACCENTRY.NUM_0 

为避免将来出现这些问题,您可以解析查询以了解问题行或消息 用户

  

CTRL + F5

或在选择查询后按检查图标

enter image description here

答案 1 :(得分:2)

您的WHERE包含3个条件,但条件需要分开,最可能由AND分隔:

FROM 
    LAMACOM.GACCENTRY, LAMACOM.GACCENTRYD, LAMACOM.PAYMENTH
WHERE
    LAMACOM.GACCENTRYD.TYP_0 = LAMACOM.GACCENTRY.TYP_0 AND
    LAMACOM.GACCENTRYD.NUM_0 = LAMACOM.GACCENTRY.NUM_0 AND
    LAMACOM.PAYMENTH.BPRVCR_0 = LAMACOM.GACCENTRY.NUM_0

或者,如果您遵循最佳做法并使用JOIN

FROM LAMACOM.GACCENTRY
JOIN LAMACOM.GACCENTRYD ON LAMACOM.GACCENTRYD.TYP_0 = LAMACOM.GACCENTRY.TYP_0
JOIN LAMACOM.PAYMENTH   ON LAMACOM.PAYMENTH.BPRVCR_0 = LAMACOM.GACCENTRY.NUM_0

这不需要WHERE,除非您想施加更多条件(然后:不要忘记AND!)

答案 2 :(得分:0)

您在第10行和第14行上有]个字符,看起来像是错字。

答案 3 :(得分:0)

您可以使用INNER JOIN

def elements = WebUiBuiltInKeywords.findWebElements(to, 5)
println elements.size()

答案 4 :(得分:0)

每个WHERE键之后必须使用AND。

WHERE
LAMACOM.GACCENTRYD.TYP_0 = LAMACOM.GACCENTRY.TYP_0 AND
LAMACOM.GACCENTRYD.NUM_0 = LAMACOM.GACCENTRY.NUM_0 AND 
LAMACOM.PAYMENTH.BPRVCR_0 = LAMACOM.GACCENTRY.NUM_0