[撤回問題:問題源於不是轉義以Web表單提交的值。 ASP腳本刪除轉義空間,從而導致該問題。]使用LIKE的SQL Server查詢在VBScript下有空格問題
在網頁中我試圖開展反對使用VBScript中的MS SQL Server中的表的搜索。搜索使用LIKE
子句(使用通配符)。如果輸入中沒有空格,搜索工作正常。但是,如果輸入中有空格,則搜索不會返回任何內容。
例如,如果我搜索IT Department
,我什麼也沒得到,但如果我搜索Department
,我會得到IT Department
。不知何故,IT
和Department
之間的空間正在引起麻煩。如果我直接反對使用SQL Server Management Studio中,使用IT Department
返回正確結果的搜索服務器運行該腳本。
另外,如果我不使用SQL佔位符參數,而是放在搜索字符串直接,搜索工作。
只有在搜索字符串包含空格和使用佔位符搜索失敗的情況。
這裏是我使用的代碼:
Dim oCmd, OffDescParam, description, query
description = "IT Department"
Set oCmd = Server.CreateObject("ADODB.Command")
OffDescParam = "%" & description & "%"
Set objOffDescParam = _
oCmd.CreateParameter("@offdesc", adChar, adParamInput, Len(OffDescParam), OffDescParam)
query = "SELECT OfficialDescription " & _
"FROM [MyDatabase].[dbo].[Organizations] " & _
"WHERE (OfficialDescription LIKE ?)" & _
"ORDER BY OfficialDescription"
' If I use "WHERE (OfficialDescription LIKE '%IT Department%')" it works.
oCmd.ActiveConnection = "some/connection/string"
oCmd.ActiveConnection.CursorLocation = adUseClient
oCmd.CommandType = adCmdText
oCmd.CommandText = query
oCmd.Parameters.Append objOffDescParam
oCmd.Prepared = True
Set oRst = CopyRecordSet(oCmd.Execute)
oCmd.ActiveConnection.close
回抽問題:這個問題從沒有逃避在Web表單提交的值出現。 ASP腳本刪除未轉義的空間,從而導致問題。 – rlandster 2010-09-04 05:47:09