1

我正在努力弄清楚如何将 T-SQL 查询转换为 MSAccess 将执行的格式。我对 MSAccess 很陌生,所以这就是我的周末。我正在尝试获取将我想要的信息拉入 MSAccess 的查询:

    SELECT p.person_id, mem.profile_id, nick_name, last_name,  p.postal_code,p.birth_date,p.gender, grade.selected_value
    FROM  dbo.core_profile_member AS mem 
    INNER JOIN dbo.core_person AS p ON mem.person_id = p.person_id  
    LEFT JOIN dbo.evnt_registrant_field AS grade ON mem.person_id = grade.person_id  AND mem.profile_id = grade.profile_id AND grade.custom_field_id=@gradeID
    WHERE mem.status_luid <> 316 AND mem.profile_id IN (@profiles)

我一直在努力解决这个问题,阅读各种帖子,试图弄清楚如何让它发挥作用。到目前为止,这是我想出的,但是当代码执行查询时会引发语法错误。这是我到目前为止不起作用的内容:

    strSQL1 = 
    "SELECT p.person_id, mem.profile_id, nick_name, last_name, " & _
    " p.postal_code, p.birth_date, p.gender FROM (" & _
    " dbo_core_profile_member AS mem INNER JOIN dbo_core_person AS p ON mem.person_id = p.person_id " & _
    " LEFT JOIN (SELECT person_id, profile_id, selected_value FROM dbo_evnt_registrant_field " & _
    " WHERE custom_field_id = " & strGradeID & ") AS grade ON mem.person_id = grade.person_id " & _
    " AND mem.profile_id = grade.profile_id) " & _
    " WHERE mem.status_luid <> 316 AND mem.profile_id IN (" & strProfileIDs & ");"

我知道变量工作正常。如果我在创建该字符串后调试并检查立即窗口,我实际上可以在 MSSQL 管理工作室中运行它并且它工作正常。我确定我错过了 SQL 生成器不喜欢的一些细微的 MSAccess 细微差别。

有人对我需要解决的问题有意见吗?使用上面的代码,我收到“查询表达式中的语法错误(缺少运算符)......”错误。

4

2 回答 2

1

Access 需要在FROM包含多个连接的子句中使用括号,并且 db 引擎对它们的位置很挑剔。

我的猜测是这个FROM条款可以工作。我用一个静态值 27 代替了strGradeID

FROM
    (dbo_core_profile_member AS mem
    INNER JOIN dbo_core_person AS p
    ON mem.person_id = p.person_id)
    LEFT JOIN
        (
            SELECT person_id, profile_id, selected_value
            FROM dbo_evnt_registrant_field
            WHERE custom_field_id = 27
        ) AS grade
    ON
            mem.person_id = grade.person_id
        AND mem.profile_id = grade.profile_id

无论我的理解是否正确,如果可能,您应该尝试使用 Access 查询设计器在新查询中设置联接。设计者知道在哪里放置括号以保持数据库引擎的运行。

一旦您在设计器中有一个工作查询,使用 strGradeID 的静态值和strProfileIDs静态值列表,修改您的 VBA 代码以生成相同的 SQL。

于 2013-07-11T18:11:41.460 回答
0
strSQL1 = 
    "SELECT p.person_id, mem.profile_id, nick_name, last_name, " & _
    " p.postal_code, p.birth_date, p.gender FROM " & _
    " dbo_core_profile_member AS mem INNER JOIN dbo_core_person AS p ON mem.person_id = p.person_id " & _
    " LEFT JOIN (SELECT person_id, profile_id, selected_value FROM dbo_evnt_registrant_field " & _
    " WHERE custom_field_id = " & strGradeID & ") AS grade ON mem.person_id = grade.person_id " & _
    " AND mem.profile_id = grade.profile_id " & _
    " WHERE mem.status_luid <> 316 AND mem.profile_id IN (" & strProfileIDs & ");"
于 2013-07-11T19:01:56.147 回答