2010-10-18 59 views
2

我有一個查詢與其他許多在同一個庫中使用的查詢幾乎相同......但我在SQL上粘貼了大量的複製&,以便爲每個都添加相似的功能稍微不一樣。下面是給我SQL分析器錯誤的部分。它發射在Set rs =線。SQL中的語法錯誤在哪裏?

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 '%'[email protected]+'%' OR " &_ 
     "LOWER([osm_description]) LIKE '%'[email protected]+'%' 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]Incorrect syntax near the keyword '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 '%'[email protected]+'%' 
    OR LOWER([osm_description]) LIKE '%'[email protected]+'%' 
    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,以及字符串(並且處於活動狀態)。然後,我需要按類別排序,以便將它們「分組」,然後在每個類別中,我需要按重量排序,然後按名稱排序。

回答

1

你錯過了第一SELECT *FROM

DECLARE @str VARCHAR(255); 
SELECT @str = LOWER(?); 
SELECT * FROM ( 
    SELECT TOP 8 * FROM [oca_search_model] 
    WHERE [osm_isactive] = 1 
    AND LOWER([osm_category]) = LOWER(?) 
    AND (
    LOWER([osm_keywords]) LIKE '%'[email protected]+'%' 
    OR LOWER([osm_description]) LIKE '%'[email protected]+'%' 
    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; 
+0

D'oh!我怎麼想那個?感謝SQL Server的有用的錯誤消息!我想在所有的複製和粘貼我設法Ctrl + X有點太多。 – sholsinger 2010-10-18 16:53:10

0

的錯誤是在本節:

AND (
    LOWER([osm_keywords]) LIKE '%'[email protected]+'%' 
    OR LOWER([osm_description]) LIKE '%'[email protected]+'%' 
    OR LOWER([osm_name]) LIKE @str+'%' 
) ORDER BY [osm_weight] DESC 
) AS T1 

的,是這裏查詢的一部分,你可以在這裏使用括號,沒有問題。 BU ORDER BY似乎添加了一組不匹配的括號。

此查詢應該(語法至少)工作:

DECLARE @str VARCHAR(255); 
SELECT @str = LOWER(?); 
SELECT TOP 8 * FROM [oca_search_model] 
WHERE [osm_isactive] = 1 
AND LOWER([osm_category]) = LOWER(?) 
AND (
    LOWER([osm_keywords]) LIKE '%'[email protected]+'%' 
    OR LOWER([osm_description]) LIKE '%'[email protected]+'%' 
    OR LOWER([osm_name]) LIKE @str+'%' 
) 
ORDER BY [osm_weight] DESC 
+0

我** **需要對頂部8​​場比賽要由'osm_weight'訂購。你的回答沒有提供對導致我的語法錯誤的答案,它似乎只是建議我刪除違規條款。 – sholsinger 2010-10-18 16:20:50

+0

@sholsinger - 違規條款*是語法錯誤。如果你只需要osm_weight的前8位,爲什麼你要按順序獲得osm_category和osm_name? – Jamiec 2010-10-18 16:28:34

+0

@sholsinger - 查看更新的答案。 – Jamiec 2010-10-18 16:30:15