结合asp和存储过程做的搜索程序
(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )
比较复杂,可以支持多种逻辑符,包括 + - 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
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
下一篇:浏览文件夹下面所有图片