Excel VBA SQL查询-查询表达式中的语法错误(缺少运算符)

时间:2018-10-09 03:28:12

标签: sql sql-server vba excel-vba

我想寻求一些调试此SQL查询的帮助。我想在Excel VBA中创建3个表/表格。

SELECT
    [A-TFN], [Title], [First Name], [Middle Name], [Last Name], [Gender],
    [Date of Birth], [Address 1], [Address 2], [City], [Postal Code], [State],
    [Employment Date], [Benefit Base Salary], [On Plan?]
FROM [Report 1$A9:P9756] e
INNER JOIN [Report 2$A11:C9761] c
    ON c.[Home NUM] = e.[Home NUM]
INNER JOIN [Report 3$A3:B6682] i
    ON i.[Employee Id] = e.[Home NUM]
WHERE (e.[Home NUM] LIKE '%123123123%') OR (e.[Host NUM] LIKE '%123123123%');

Set obj_res = obj_con.Execute(str_sqlquery)

在联接2个表时它工作正常,但是当我为报表3添加INNER JOIN时,在查询表达式中出现了语法错误(缺少运算符)。

此部分显示错误

  

c。[Home NUM] = e。[Home NUM] INNER JOIN [Report 3 $ A3:B6682] i ON   i。[员工ID] = e。[首页NUM]

谢谢。

1 个答案:

答案 0 :(得分:2)

在Access SQL语法中,您需要将第一个内部联接放在括号中,如下所示:

SELECT
    [A-TFN], [Title], [First Name], [Middle Name], [Last Name], [Gender],
    [Date of Birth], [Address 1], [Address 2], [City], [Postal Code], [State],
    [Employment Date], [Benefit Base Salary], [On Plan?]
FROM
(
    [Report 1$A9:P9756] e
    INNER JOIN [Report 2$A11:C9761] c
        ON c.[Home NUM] = e.[Home NUM]
)
INNER JOIN [Report 3$A3:B6682] i
    ON i.[Employee Id] = e.[Home NUM]
WHERE (e.[Home NUM] LIKE '%123123123%') OR (e.[Host NUM] LIKE '%123123123%');