比較複雜,可以支援多種邏輯符,包括 + - and or 空格等,並且根據需要隨便增加。可以根據條件選擇在那個表中搜尋,對速度也做了最佳化,可以說是很快的。當然因為是以前寫的,存在不少毛病。預存程序中用到幾乎所有sql server的特性,如游標(記錄集分頁)等。好了,不吹了,自己看程式吧。
asp 函數 複製代碼 代碼如下: function AnalyseKeyword(a_strSource)
dim m_strDest , m_intLoop
dim m_intBeginPos , m_intEndPos
dim m_strHead , m_strMiddle , m_strTail
m_strDest = a_strSource
'------------------------------處理空格------------------------------------------------------
'首先去迴轉尾空格
m_strDest = ltrim(rtrim(m_strDest))
'將& , " and " 等替換成 +、 -、空格
m_strDest = replace(m_strDest , "&" , "+")
m_strDest = replace(m_strDest , " AND " , "+")
m_strDest = replace(m_strDest , " OR " , chr(32))
m_strDest = replace(m_strDest , " NOT " , "-")
'初始設定變數,以使下面的迴圈進行
m_intBeginPos = 1
do while m_intBeginPos <> 0
m_intBeginPos = instr(m_strDest ,chr(32))
if m_intBeginPos <> 0 then '如果找到空格
m_strHead = rtrim(ltrim(left ( m_strDest , m_intBeginPos )))
call print("[AnalyseKeyword()]:處理空格m_strHead = " + m_strHead)
m_strTail = rtrim(ltrim(right (m_strDest , len(m_strDest) - m_intBeginPos)))
call print("[AnalyseKeyword()]:處理空格m_strTail = " + m_strTail)
m_strDest = m_strHead + "*" + m_strTail
else
exit do
end if
loop
m_strDest = replace (m_strDest , "*" , chr(32))
call print("[AnalyseKeyword()]:處理空格完畢後m_strDest = " + m_strDest)
'-------------------------------空格處理完畢-------------------------------------------------
'-------------------處理單雙引號-----------------------------------------------------
'首先將單引號替換為雙引號
m_strDest = replace ( m_strDest , chr(39) , chr(34))
'置一個初值以使迴圈進行
m_intBeginPos = 1
m_intEndPos =1
m_strHead = ""
m_strTail = ""
do while m_intBeginPos <> 0 and m_intEndPos <> 0
'如果發現雙引號,則記下開始位置,尋找下一個雙引號
m_intBeginPos = instr(m_strDest , chr(34))
if m_intBeginPos <> 0 then '如果找到第一個引號
call print("[AnalyseKeyword()]:第一個引號出現的位置:" + cstr(m_intBeginPos))
m_intEndPos = instr(m_intBeginPos + 1 , m_strDest ,chr(34))
if m_intEndPos <> 0 then '如果找到第二個引號
call print("[AnalyseKeyword()]:第二個引號出現的位置:" + cstr(m_intEndPos))
'將整個字串按引號分隔成三段
call print ("[AnalyseKeyword()]:處理引號m_strDest = " + m_strDest)
m_strHead = left(m_strDest , m_intBeginPos - 1)
call print ("[AnalyseKeyword()]:處理引號m_strHead = " + m_strHead)
m_strMiddle = mid(m_strDest , m_intBeginPos + 1 , m_intEndPos - m_intBeginPos - 1)
call print ("[AnalyseKeyword()]:處理引號m_strMiddle = " + m_strMiddle)
m_strTail = right(m_strDest , len(m_strDest) - m_intEndPos)
call print ("[AnalyseKeyword()]:m_strTail = " + m_strTail)
'如果在引號中有+號則作為字元處理,暫時替換成其他字元
m_strMiddle = replace(m_strMiddle , "+" , "|")
m_strDest = m_strHead + replace(rtrim(ltrim(m_strMiddle)) , chr(32) , "#") + m_strTail
else
exit do
end if
else
exit do
end if
loop
m_strDest = replace(m_strDest , chr(34) , "+")
call print ("[AnalyseKeyword()]:處理引號完畢後m_strDest = " + m_strDest)
'-------------------------------引號處理完畢-------------------------------------------------
'-------------------------------處理多個加號及加號兩邊的空格問題-----------------------------
'處理多個加號的問題,遇到多個加號則認為是字串,而不是邏輯符
m_strDest = replace (m_strDest , "+++" ,"|||")
m_strDest = replace (m_strDest , "++" , "||")
call print ("[AnalyseKeyword()]:處理多個減號完畢後m_strDest = '" + m_strDest + "'")
'處理加號兩邊的空格
m_strDest = replace(m_strDest , " +" , "+")
m_strDest = replace(m_strDest , "+ " , "+")
m_strDest = replace(m_strDest , " + " , "+")
call print ("[AnalyseKeyword()]:處理減號兩邊的空格完畢後m_strDest = '" + m_strDest + "'")
'-------------------------------處理加號完畢-----------------------------
'-------------------------------處理多個減號及減號兩邊的空格問題-----------------------------
'處理多個減號的問題,遇到多個減號則認為是字串,而不是邏輯符
m_strDest = replace (m_strDest , "---" ,"~~~")
m_strDest = replace (m_strDest , "--" , "~~")
call print ("[AnalyseKeyword()]:處理多個減號完畢後m_strDest = '" + m_strDest + "'")
'處理減號兩邊的空格
m_strDest = replace(m_strDest , " -" , "-")
m_strDest = replace(m_strDest , "- " , "-")
m_strDest = replace(m_strDest , " - " , "-")
call print ("[AnalyseKeyword()]:處理加號兩邊的空格完畢後m_strDest = '" + m_strDest + "'")
'-------------------------------處理減號完畢-----------------------------
'------------------------------處理字串兩頭的加減號問題-----------------
if len(m_strDest) >= 3 then
m_strHead = left(m_strDest , 1)
m_strMiddle = mid(m_strDest , 2 , len(m_strDest) - 2)
m_strTail = right(m_strDest , 1)
if m_strHead = "+" or m_strHead = "-" then
m_strHead = ""
end if
if m_strTail = "+" or m_strTail = "-" then
m_strTail = ""
end if
m_strDest = m_strHead + m_strMiddle + m_strTail
end if
'----------------------------處理完畢-------------------------------------
m_strDest = replace(m_strDest , "--" , "~~")
m_strDest = replace(m_strDest , "++" , "||")
m_strDest = replace(m_strDest , chr(32) , "@")
AnalyseKeyword = m_strDest
call print ("[AnalyseKeyword()]:全部處理完畢後m_strDest = '" + m_strDest + "'")
end function
%>
預存程序
/*********************************************************************/
/* proc name : Up_ParseWordSearch */
/* */
/* Description: 關鍵字搜尋 */
/* */
/* parameters: @a_strCategoryID 分類id */
/* @a_intPosition 調用的位置 */
/* @a_strParseWord 搜尋索引鍵 */
/* @a_intRowCount 限定最多取得記錄數 */
/* */
/* date: 2000/6/28 */
/* */
/* author: Liuyunpeng */
/* */
/* history: */
/*********************************************************************/
if exists (select * from sysobjects where id = object_id("up_ParseWordSearch"))
drop proc up_ParseWordSearch
go
create proc up_ParseWordSearch @a_strParseword varchar(255) ,
@a_strCategoryID varchar(255) ,
@a_intPosition tinyint ,
@a_intRowCount int
as
declare @m_strSqlCondition varchar(255) --Sql語句的條件部分
declare @m_strSqlSelect varchar(255) --Sql語句的選擇部分
declare @m_strSqlCategory varchar(100) --sql語句的分類部分
/*根據調用位置決定sql的選擇部分*/
select @m_strSqlSelect
= case
when @a_intPosition = 4 then --商品庫
"select ProductID , 'Title' = ProductName , 'Description' = left(Description , 100) "
+ " from Product where "
when @a_intPosition = 5 then --商業機會庫
"select ID , Title ,'Description' = left(convert(varchar,content) , 100) "
+ " from BusinessChance where "
when @a_intPosition = 6 then --公司庫
"select CompanyID , 'Title' = CompanyName , 'Description' =left(Description , 100) "
+ " from Company where "
end
/*根據分類ID決定sql的分類部分*/
select @m_strSqlCategory
= case
when @a_strCategoryID <> "0" then " CategoryID like '" + @a_strCategoryID + "%' and "
else ""
end
/*根據調用位置決定sql的條件部分*/
select @m_strSqlCondition
= case
when @a_intPosition = 4 --商品
then "(ProductName like '%" + @a_strParseWord + "%'"
+ " or Description like '%" + @a_strParseWord + "%'"
+ " or ProducerName like '%" + @a_strParseWord + "%') "
when @a_intPosition = 5 --商業機會
then "(Title like '%" + @a_strParseWord + "%'"
+ " or Keyword like '%" + @a_strParseWord + "%') "
when @a_intPosition = 6
then "(CompanyName like '%" + @a_strParseWord + "%'"
+ " or Description '%" + @a_strParseWord + "%') "
end
set rowcount @a_intRowCount
exec (@m_strSqlSelect + @m_strSqlCategory + @m_strSqlCondition)
set rowcount 0
go