当我运行下面的查询时,我在查询表达式中出现语法错误。当我使用 sqlclient 并从 sql 服务器.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 - 生产订单详情]。针织订单ID = [KN - 针织订单]。KnittingOrderID INNER JOIN [GN - EntityMaster] ON [GN - 实体主]。实体 ID = [KN - 针织订单]。实体 ID 内部 加入 [KN - 特殊说明大师] 在 [KN - speci' 上。
源 =Microsoft 访问数据库引擎堆栈跟踪:
提前谢谢你
只需添加一点格式并使用别名,这堵文本墙就变得非常易于管理。我会尝试弄清楚您是否可以更改这些表名称,以便它们没有空格和其他保留字符。这在 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;