我有一个交叉表查询,如下所示:
TRANSFORM Sum([Raw Scores].[Raw Score]) AS SumOfScore
SELECT [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number], Sum([Raw Scores].Score) AS [Final Score], Sum([Raw Scores].[Raw Score]) AS [Total Shots]
FROM [Raw Scores]
WHERE [Round Number] = [Enter Round Number:]
GROUP BY [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number]
PIVOT [Raw Scores].[Hole ID];
[Raw Scores].[Round Number]
是另一个表格中的一列,其中包含数据
但是,this.[Enter Round Number:]
是一个参数。
我要做的是按照他们获得的回合对所有得分进行排序,然而,WHERE
条款似乎有问题。
每当使用非列名称时,它都会发出以下错误:
但是,当使用一个确定的值(一个写在查询中而不是一个参数)时,查询运行得非常好。
TRANSFORM Sum([Raw Scores].[Raw Score]) AS SumOfScore
SELECT [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number], Sum([Raw Scores].Score) AS [Final Score], Sum([Raw Scores].[Raw Score]) AS [Total Shots]
FROM [Raw Scores]
WHERE [Round Number] = 1
GROUP BY [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number]
PIVOT [Raw Scores].[Hole ID];
注意: [Raw Scores]
是一个查询。将带有参数的WHERE
子句添加到该查询会产生完美的结果,但是当与上述查询一起使用时,它会显示错误消息(如上图所示)......
这是[Raw Scores]
查询,其参数有效(并且其中包含未知变量):
SELECT Scores.PlayerID, Scores.Score AS [Raw Score], Scores.[Hole ID], Holes.Par, IIf([Raw Score]=0,Holes.Par,[Raw Score]-Holes.Par) AS Score, Players.[First Name], Players.[Last Name], Scores.[Round Number], Players.Country
FROM (Scores INNER JOIN Holes ON Scores.[Hole ID] = Holes.[Hole Number]) INNER JOIN Players ON Scores.PlayerID = Players.[Player ID]
WHERE [Round Number] = [Enter Round Number:];
更新:未将参数定义为字段似乎可以解决问题,但为了便于阅读,我仍然需要显示正确的名称。
TRANSFORM Sum([Raw Scores].[Raw Score]) AS SumOfScore
SELECT [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number], Sum([Raw Scores].Score) AS [Final Score], Sum([Raw Scores].[Raw Score]) AS [Total Shots]
FROM [Raw Scores]
WHERE [Round Number] = ?
GROUP BY [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number]
PIVOT [Raw Scores].[Hole ID];
如何让查询正常工作;要求价值?
像这样:答案 0 :(得分:1)
问题在于
WHERE [Round Number] = [Enter Round Number:]
然后它将[输入Round Number:]视为表达式或值,看起来它们都不是它们,因此您得到了您提到的错误。
您需要为[Enter Round Number:]表达式提供适当的值。您可以在参数中传递值,然后就可以使用它。您可以使用QueryDefs
,然后使用参数创建查询。
Imports Data.OleDb
dim cmd as new OleDbCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number], Sum([Raw Scores].Score) AS [Final Score], Sum([Raw Scores].[Raw Score]) AS [Total Shots]
FROM [Raw Scores]
WHERE [RoundNumber] = ?
GROUP BY [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores]. [Last Name], [Raw Scores].[Round Number]
PIVOT [Raw Scores].[Hole ID];"
cmd.Parameters.Add("@RoundNumber", OleDbType.VarChar).value = "RoundNumber"
Dim da as new OleDbDataAdapter(cmd)
答案 1 :(得分:0)
显然,你所要做的就是在检查它是否是一个字段时将其嵌套以清除混淆。
PARAMETERS [Enter Round Number:] Value;
TRANSFORM Sum([Raw Scores].[Raw Score]) AS SumOfScore
SELECT [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number], Sum([Raw Scores].Score) AS [Final Score], Sum([Raw Scores].[Raw Score]) AS [Total Shots]
FROM [Raw Scores]
WHERE ((([Raw Scores].[Round Number])=[Enter Round Number:]))
GROUP BY [Raw Scores].PlayerID, [Raw Scores].[First Name], [Raw Scores].[Last Name], [Raw Scores].[Round Number]
PIVOT [Raw Scores].[Hole ID];