简体   繁体   English

访问VBA错误3075语法错误(缺少运算符)

[英]Access VBA Error 3075 Syntax Error (missing operator)

I seem to continually run in to problems when setting a string as and SQL statement I had pre-written in SSMS. 在将字符串设置为和我在SSMS中预先编写的SQL语句时,我似乎不断遇到问题。

I have 2 questions which are both related. 我有两个相关的问题。 First, I only seem to run in to problems when dealing with a bit more complex queries (several joins, where conditions, correlated sub queries, and case statements) as a opposed to simple queries. 首先,相对于简单查询,我似乎只遇到了一些更复杂的查询(几个联接,条件,相关子查询和case语句)的问题。 Anyone have any good resources where I can read about syntax 'rules' when writing queries in SSMS to VBA? 在将SSMS中的查询写入VBA时,有人有什么好的资源可供我阅读有关语法“规则”的信息?

Second, below is an example of a query I wrote in the AdventureWorks sample DB that gives me the error listed in the title of this question. 其次,下面是我在AdventureWorks示例数据库中编写的查询示例,该查询为我提供了此问题标题中列出的错误。 My T-SQL statement is: 我的T-SQL语句是:

sSQL = "SELECT ba.BusinessEntityID,ba.AddressID
              ,(CASE WHEN ba.BusinessEntityID > 5
                THEN
                (SELECT pp.FirstName FROM [Person].[Person] pp
                WHERE pp.BusinessEntityID = ba.BusinessEntityID)
               Else 'AA' END) AS 'TEST'
        FROM Person.BusinessEntityAddress ba
        INNER JOIN person.AddressType at ON at.AddressTypeID = ba.AddressTypeID
        WHERE ba.BusinessEntityID < 11;"

Error occurs on line "Set rs = db.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)" which says: 在“ Set rs = db.OpenRecordset(sSQL,dbOpenDynaset,dbSeeChanges)”行上发生错误,该行显示:

"Run-Time Error 3075 Syntax Error (missing operator) in query expression '(CASE WHEN ba.BusinessEntityID > 5 THEN (SELECT pp.FirstName FROM [Person].[Person] pp WHERE pp.BusinessEntityID = ba.BusinessEntityID) Else 'AA' END) AS 'TEST'" “查询表达式中的运行时错误3075语法错误(缺少运算符)'(案例ba.BusinessEntityID> 5然后,(从[Person]中选择pp.FirstName。[Person] pp WHERE pp.BusinessEntityID = ba.BusinessEntityID)否则' AA'END)AS'TEST'”

Try this 尝试这个

sSQL = "SELECT ba.BusinessEntityID,ba.AddressID
          ,(CASE WHEN ba.BusinessEntityID > 5
            THEN
            (SELECT pp.FirstName FROM [Person].[Person] pp
            WHERE pp.BusinessEntityID = ba.BusinessEntityID)
           Else 'AA' END) AS TEST 
    FROM Person.BusinessEntityAddress ba
    INNER JOIN person.AddressType at ON at.AddressTypeID = ba.AddressTypeID
    WHERE ba.BusinessEntityID < 11;"

No need to using quotes in alias name. 无需在别名中使用引号。 If you must, then there are other ways (like drop AS or use Double quotes) but it is not suggested. 如果必须的话,还有其他方法(例如,删除AS或使用双引号),但不建议这样做。

The issue here is that you are trying to use a TSQL statement where an Access SQL statement is expected. 这里的问题是,您正在尝试在期望访问SQL语句的地方使用TSQL语句。 Access SQL does not support CASE WHEN , or many of the constructs that TSQL supports. Access SQL不支持CASE WHEN或TSQL支持的许多构造。

The OpenRecordset method expects a table name, saved query name, or Access SQL statement. OpenRecordset方法需要一个表名,保存的查询名或Access SQL语句。

You can solve this by creating and saving a Pass Through Query in Microsoft Access. 您可以通过在Microsoft Access中创建和保存传递查询来解决此问题。 When you create a Pass Through Query, you can then use your TSQL statement, and you set the connection string to your SQL database in the properties of the Pass Through Query. 创建传递查询时,可以使用TSQL语句,并在传递查询的属性中将连接字符串设置为SQL数据库。

As the name implies, Access passes the parsing and execution of a Pass Through Query over to SQL Server. 顾名思义,Access 传递查询的解析和执行传递给SQL Server。

Once you have saved your Pass Through Query and tested it, you can use your OpenRecordset command as-is, just replace the first parameter with the name of the Pass Through Query. 保存传递查询并对其进行测试后,可以按原样使用OpenRecordset命令,只需将第一个参数替换为传递查询的名称即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM