我正在试图弄清楚如何将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细微差别。
任何人都有关于我需要修复的内容的输入?使用上面的代码,我在查询表达式中得到“语法错误(缺少运算符)...”错误。
答案 0 :(得分:1)
Access需要FROM
子句中的括号,其中包含多个连接,并且db引擎对其位置非常挑剔。
我猜这个FROM
条款可以起作用。我为 strGradeID 替换了静态值27。
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。
答案 1 :(得分: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 & ");"