此 SQL 中的语法错误在哪里?
我有一个查询与同一个库中使用的许多其他查询几乎相同...但是我在 SQL 上进行了大量的复制和粘贴,以向每个查询添加功能,这些功能都相似但略有不同。下面是给我 SQL 解析器错误的部分。它在 Set rs =
行触发。
dim sql, rs
sql = "DECLARE @str VARCHAR(255); SELECT @str = LOWER(?);" &_
"SELECT * ( " &_
"SELECT TOP 8 * FROM [oca_search_model] WHERE " &_
"[osm_isactive] = 1 AND " &_
"LOWER([osm_category]) = LOWER(?) AND " &_
"(LOWER([osm_keywords]) LIKE '%'+@str+'%' OR " &_
"LOWER([osm_description]) LIKE '%'+@str+'%' OR " &_
"LOWER([osm_name]) LIKE @str+'%') " &_
"ORDER BY [osm_weight] DESC" &_
") AS T1 ORDER BY [T1].[osm_category] ASC, [osm_weight] DESC, [osm_name] ASC;"
Set rs = executeQuery(conn, sql, Array(searchString, category))
我收到的具体错误是:[Microsoft][ODBC SQL Server Driver][SQL Server]关键字“ORDER”附近的语法不正确。
我已打印出从该串联生成的 SQL,它如下(添加换行符):
DECLARE @str VARCHAR(255);
SELECT @str = LOWER(?);
SELECT * (
SELECT TOP 8 * FROM [oca_search_model]
WHERE [osm_isactive] = 1
AND LOWER([osm_category]) = LOWER(?)
AND (
LOWER([osm_keywords]) LIKE '%'+@str+'%'
OR LOWER([osm_description]) LIKE '%'+@str+'%'
OR LOWER([osm_name]) LIKE @str+'%'
)
ORDER BY [osm_weight] DESC
) AS T1
ORDER BY [T1].[osm_category] ASC, [osm_weight] DESC, [osm_name] ASC;
为了供您参考,我在下面添加了 executeQuery
函数。
Function executeQuery(ByRef connection, ByRef querytext, ByRef parameters)
Dim cmd, i, rs
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = querytext
cmd.CommandType = 1
cmd.Prepared = True
For i = 0 To UBound(parameters)
cmd.Parameters.Append(createVarCharInputParameter(cmd, "", parameters(i)))
Next
Set cmd.ActiveConnection = connection
Set rs = cmd.Execute()
Set executeQuery = rs
End Function
我无权使用一些测试值直接在服务器上运行查询。但是没有 LOWER([osm_category]) = LOWER(?) AND 部分的类似查询运行得很好。你能发现该 SQL 中的语法错误吗?我和我的同事们似乎都做不到。
请注意,我必须按 osm_weight
字段保留前 8 条记录的排序。更具体地说,查询需要:按权重获取前 8 条记录匹配类别和字符串(并且处于活动状态)。然后我需要按类别对它们进行排序,以便将它们“分组”,然后在每个类别中我需要按重量排序,然后按名称排序。
I've got a query that is pretty much the same as many others which are used in the same library... but I did a lot of copy&paste on the SQL to add features to each one which are all similar but slightly different. Just below is the section which gives me the SQL Parser error. It fires at the Set rs =
line.
dim sql, rs
sql = "DECLARE @str VARCHAR(255); SELECT @str = LOWER(?);" &_
"SELECT * ( " &_
"SELECT TOP 8 * FROM [oca_search_model] WHERE " &_
"[osm_isactive] = 1 AND " &_
"LOWER([osm_category]) = LOWER(?) AND " &_
"(LOWER([osm_keywords]) LIKE '%'+@str+'%' OR " &_
"LOWER([osm_description]) LIKE '%'+@str+'%' OR " &_
"LOWER([osm_name]) LIKE @str+'%') " &_
"ORDER BY [osm_weight] DESC" &_
") AS T1 ORDER BY [T1].[osm_category] ASC, [osm_weight] DESC, [osm_name] ASC;"
Set rs = executeQuery(conn, sql, Array(searchString, category))
The specific error I receive is: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'ORDER'.
I have printed out the SQL that is generated from that concatenation and it is as follows (with added line breaks):
DECLARE @str VARCHAR(255);
SELECT @str = LOWER(?);
SELECT * (
SELECT TOP 8 * FROM [oca_search_model]
WHERE [osm_isactive] = 1
AND LOWER([osm_category]) = LOWER(?)
AND (
LOWER([osm_keywords]) LIKE '%'+@str+'%'
OR LOWER([osm_description]) LIKE '%'+@str+'%'
OR LOWER([osm_name]) LIKE @str+'%'
)
ORDER BY [osm_weight] DESC
) AS T1
ORDER BY [T1].[osm_category] ASC, [osm_weight] DESC, [osm_name] ASC;
For your reference, I have added the executeQuery
function below.
Function executeQuery(ByRef connection, ByRef querytext, ByRef parameters)
Dim cmd, i, rs
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = querytext
cmd.CommandType = 1
cmd.Prepared = True
For i = 0 To UBound(parameters)
cmd.Parameters.Append(createVarCharInputParameter(cmd, "", parameters(i)))
Next
Set cmd.ActiveConnection = connection
Set rs = cmd.Execute()
Set executeQuery = rs
End Function
I don't have access to run the query directly on the server with some test values. But a similar query without the LOWER([osm_category]) = LOWER(?) AND
portion runs just fine. Can you spot the syntax error in that SQL? My colleagues and I can't seem to.
Please note that it is imperative that I retain the ordering of the top 8 records by the osm_weight
field. More specifically, the query needs to: get the top 8 by weight that match the category, and string (and are active). Then I need to order them by category so they're "grouped" and then within each category I need them ordered by weight and then by name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在第一个
SELECT *
之后缺少FROM
。You're missing the
FROM
after the firstSELECT *
.错误就在这一段:
AND是where查询的一部分,这里可以使用括号,没问题。但是 ORDER BY 似乎添加了一组不匹配的括号。
这个查询应该(至少在语法上)有效:
The error is in this section:
The AND is part of the where query, and you can use parenthesis here, no problem. Bu the ORDER BY seems to add a set of mismatched Parenthesis.
This query should (syntactically at least) work: