【问题标题】:can't find Missing operator sql oledb vb.net [duplicate]找不到缺少的运算符 sql oledb vb.net [重复]
【发布时间】:2023-03-23 23:58:02
【问题描述】:

当我运行以下查询时,查询表达式中出现语法错误。 当我使用 sqlclient 并从 sql server .mdf 文件读取时,这个确切的代码有效。但是现在我正在从一个 ms 访问 .mdb 文件中读取,它给了我一个缺少运算符的错误

Dim Adapter As New OleDbDataAdapter
Dim Data As New DataTable
Dim SQL As String
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Shantara Production IT.mdb")
Dim cmd As New OleDbCommand()
grdvbatchprodn.Visible = True
SQL = "SELECT [KN - ProductionOrderDetails].BatchNo, 
              [GN - EntityMaster].EntityName, 
              [FG - End Product Codes].ProductCode,
              SUM([KN - ProductionOrderDetails].ProductionQty) AS [Batch Total Panels],
              [KN -Special Instructions Master].SpecialInstructionDetail,
              [KN - ProductionOrderHeader].KnittBatchComplete 
        FROM [KN - ProductionOrderDetails] INNER JOIN [KN - KnittingOrder]      
            ON  [KN - ProductionOrderDetails].KnittingOrderID = [KN - KnittingOrder].KnittingOrderID
        INNER JOIN [GN - EntityMaster] 
            ON [GN - EntityMaster].EntityID = [KN - KnittingOrder].EntityID 
        INNER JOIN [KN -Special Instructions Master] 
            ON [KN -Special Instructions Master].SpecialInstructionID = [KN - KnittingOrder].SpecialInstructionID
        INNER JOIN [KN - ProductionOrderHeader] 
            ON [KN - ProductionOrderHeader].BatchNo = [KN - ProductionOrderDetails].BatchNo 
        INNER JOIN [FG - End Product Codes] 
            ON [FG - End Product Codes].ProductID = [KN - ProductionOrderHeader].ProductID
        INNER JOIN [KN - KnittingDetailsHeader] 
            ON [KN - KnittingDetailsHeader].BatchNo = [KN - ProductionOrderDetails].BatchNo
        WHERE [KN - ProductionOrderHeader].KnittBatchComplete = 0
        GROUP BY [KN - ProductionOrderDetails].BatchNo, [GN - EntityMaster].EntityName, [FG - End Product Codes].ProductCode, [KN -Special Instructions Master].SpecialInstructionDetail, [KN - ProductionOrderHeader].KnittBatchComplete
        ORDER BY [KN - ProductionOrderDetails].BatchNo, [GN - EntityMaster].EntityName;"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = SQL

        Adapter.SelectCommand = cmd
        Adapter.Fill(Data)

        grdvbatchprodn.DataSource = Data
        grdvbatchprodn.DataBind()
End Sub

错误是:

System.Data.OleDb.OleDbException 发生 HResult=0x80040E14
查询表达式'[KN - 中的消息=语法错误(缺少运算符) ProductionOrderDetails].KnittingOrderID = [KN - KnittingOrder].KnittingOrderID INNER JOIN [GN - EntityMaster] ON [GN - EntityMaster].EntityID = [KN - KnittingOrder].EntityID INNER JOIN [KN -Special Instructions Master] ON [KN -Speci'.
Source=Microsoft Access 数据库引擎 StackTrace:

提前谢谢你

【问题讨论】:

  • 哎哟....那些表名让我想拿出我的眼球并弹出它们。至少要学会如何使用别名,这样你就不必不断重复那些讨厌的事情了。
  • 逐步从查询中删除部分。当问题消失时,您就找到了问题行。
  • @SeanLange 我最初确实使用了别名,但问题是 ms 访问不支持它们所以我不得不这样做
  • 什么??? Access 支持表别名。您只需要包含在 sql server 中可选的 AS 关键字。不得不问为什么会选择从sql server中取数据放到Access中。
  • @SeanLange 感谢“AS”的帮助我不知道..因为我正在构建的系统只是程序的一部分..所有其他形式都内置在当前访问中我发现问题在于您需要使用括号的多个 INNER JOINS

标签: sql asp.net sql-server vb.net oledb


【解决方案1】:

只需添加一点格式并使用别名,这堵文字墙就变得非常易于管理。我会尝试弄清楚您是否可以更改这些表名,以便它们没有空格和其他保留字符。这在 Access 中应该可以正常工作。

SELECT pod.BatchNo, 
    em.EntityName, 
    epcProductCode,
    SUM(pod.ProductionQty) AS [Batch Total Panels],
    sim.SpecialInstructionDetail,
    poh.KnittBatchComplete 
FROM [KN - ProductionOrderDetails] as pod
INNER JOIN [KN - KnittingOrder] as ko ON  pod.KnittingOrderID = ko.KnittingOrderID
INNER JOIN [GN - EntityMaster] as em ON em.EntityID = ko.EntityID 
INNER JOIN [KN -Special Instructions Master] as sim ON sim.SpecialInstructionID = ko.SpecialInstructionID
INNER JOIN [KN - ProductionOrderHeader] as poh ON poh.BatchNo = pod.BatchNo 
INNER JOIN [FG - End Product Codes] as epc ON epcProductID = poh.ProductID
INNER JOIN [KN - KnittingDetailsHeader] kdh ON kdhBatchNo = pod.BatchNo
WHERE poh.KnittBatchComplete = 0
GROUP BY pod.BatchNo
    , em.EntityName
    , epcProductCode
    , sim.SpecialInstructionDetail
    , poh.KnittBatchComplete
ORDER BY pod.BatchNo
    , em.EntityName;

【讨论】:

  • Access 要求每个连接子句都用括号括起来,因此无论是否使用别名都需要添加。查看我标记的副本。
  • @DStanley 好的。我还没有在 Access 中写过查询……嗯……很长一段时间。用括号括住一个连接子句的要求相当奇怪,但无论如何,我将继续不使用 Access。
  • @SeanLange 他的权利,它现在可以工作了
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多