结合asp和存储过程做的搜索程序
程序员文章站
2022-05-03 12:02:08
比较复杂,可以支持多种逻辑符,包括 + - and or 空格等,并且根据需要随便增加。可以根据条件选择在那个表中搜索,对...
比较复杂,可以支持多种逻辑符,包括 + - 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