佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 結(jié)合asp和存儲(chǔ)過(guò)程做的搜索程序

結(jié)合asp和存儲(chǔ)過(guò)程做的搜索程序

熱門(mén)標(biāo)簽:團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 阿里云
比較復(fù)雜,可以支持多種邏輯符,包括 + - and or 空格等,并且根據(jù)需要隨便增加??梢愿鶕?jù)條件選擇在那個(gè)表中搜索,對(duì)速度也做了優(yōu)化,可以說(shuō)是很快的。當(dāng)然因?yàn)槭且郧皩?xiě)的,存在不少毛病。存儲(chǔ)過(guò)程中用到幾乎所有sql server的特性,如光標(biāo)(記錄集分頁(yè))等。好了,不吹了,自己看程序吧。

asp 函數(shù)
復(fù)制代碼 代碼如下:

  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 " , "-")  

      '初始化變量,以使下面的循環(huán)進(jìn)行
      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) 
      '-------------------------------空格處理完畢-------------------------------------------------

      '-------------------處理單雙引號(hào)-----------------------------------------------------

      '首先將單引號(hào)替換為雙引號(hào)
      m_strDest = replace ( m_strDest , chr(39) , chr(34))      

      '置一個(gè)初值以使循環(huán)進(jìn)行
      m_intBeginPos = 1
      m_intEndPos   =1
      m_strHead = ""
      m_strTail = ""
      do while m_intBeginPos > 0 and m_intEndPos > 0
         '如果發(fā)現(xiàn)雙引號(hào),則記下開(kāi)始位置,查找下一個(gè)雙引號(hào)
         m_intBeginPos = instr(m_strDest , chr(34))
         if m_intBeginPos > 0 then          '如果找到第一個(gè)引號(hào)

            call print("[AnalyseKeyword()]:第一個(gè)引號(hào)出現(xiàn)的位置:" + cstr(m_intBeginPos))

            m_intEndPos = instr(m_intBeginPos + 1 , m_strDest ,chr(34))
            if m_intEndPos > 0 then         '如果找到第二個(gè)引號(hào)

               call print("[AnalyseKeyword()]:第二個(gè)引號(hào)出現(xiàn)的位置:" + cstr(m_intEndPos))

               '將整個(gè)字符串按引號(hào)分隔成三段
               call print ("[AnalyseKeyword()]:處理引號(hào)m_strDest = " + m_strDest)
               m_strHead   = left(m_strDest , m_intBeginPos - 1)
               call print ("[AnalyseKeyword()]:處理引號(hào)m_strHead = " + m_strHead)
               m_strMiddle = mid(m_strDest , m_intBeginPos + 1 , m_intEndPos - m_intBeginPos - 1)
               call print ("[AnalyseKeyword()]:處理引號(hào)m_strMiddle = " + m_strMiddle)
               m_strTail   = right(m_strDest , len(m_strDest) - m_intEndPos) 
               call print ("[AnalyseKeyword()]:m_strTail = " + m_strTail)

               '如果在引號(hào)中有+號(hào)則作為字符處理,暫時(shí)替換成其他字符
               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()]:處理引號(hào)完畢后m_strDest = " + m_strDest)

      '-------------------------------引號(hào)處理完畢-------------------------------------------------

      '-------------------------------處理多個(gè)加號(hào)及加號(hào)兩邊的空格問(wèn)題-----------------------------

      
      '處理多個(gè)加號(hào)的問(wèn)題,遇到多個(gè)加號(hào)則認(rèn)為是字符串,而不是邏輯符
      m_strDest = replace (m_strDest , "+++" ,"|||")
      m_strDest = replace (m_strDest , "++" , "||")
      call print ("[AnalyseKeyword()]:處理多個(gè)減號(hào)完畢后m_strDest = '" + m_strDest + "'")

      '處理加號(hào)兩邊的空格
      m_strDest = replace(m_strDest , " +" , "+")
      m_strDest = replace(m_strDest , "+ " , "+")
      m_strDest = replace(m_strDest , " + " , "+")
      call print ("[AnalyseKeyword()]:處理減號(hào)兩邊的空格完畢后m_strDest = '" + m_strDest + "'")

      '-------------------------------處理加號(hào)完畢-----------------------------

      '-------------------------------處理多個(gè)減號(hào)及減號(hào)兩邊的空格問(wèn)題-----------------------------

      
      '處理多個(gè)減號(hào)的問(wèn)題,遇到多個(gè)減號(hào)則認(rèn)為是字符串,而不是邏輯符
      m_strDest = replace (m_strDest , "---" ,"~~~")
      m_strDest = replace (m_strDest , "--" , "~~")

      call print ("[AnalyseKeyword()]:處理多個(gè)減號(hào)完畢后m_strDest = '" + m_strDest + "'")

      '處理減號(hào)兩邊的空格
      m_strDest = replace(m_strDest , " -" , "-")
      m_strDest = replace(m_strDest , "- " , "-")
      m_strDest = replace(m_strDest , " - " , "-")
      call print ("[AnalyseKeyword()]:處理加號(hào)兩邊的空格完畢后m_strDest = '" + m_strDest + "'")

      '-------------------------------處理減號(hào)完畢-----------------------------

      '------------------------------處理字符串兩頭的加減號(hào)問(wèn)題-----------------
      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     
%>  


存儲(chǔ)過(guò)程
/*********************************************************************/
/* proc name :    Up_ParseWordSearch                                 */
/*                                                                   */
/* Description:   關(guān)鍵字搜索                                         */
/*                                                                   */
/* parameters:    @a_strCategoryID   分類id                          */
/*                @a_intPosition     調(diào)用的位置                      */
/*                @a_strParseWord    搜索關(guān)鍵字                      */
/*                @a_intRowCount     限定最多取得記錄數(shù)              */
/*                                                                   */
/* 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語(yǔ)句的條件部分
      declare @m_strSqlSelect     varchar(255)       --Sql語(yǔ)句的選擇部分
      declare @m_strSqlCategory   varchar(100)       --sql語(yǔ)句的分類部分

      /*根據(jù)調(diào)用位置決定sql的選擇部分*/
      select @m_strSqlSelect 
             = case 
                   when @a_intPosition = 4 then                  --商品庫(kù)
                         "select ProductID , 'Title' = ProductName , 'Description' = left(Description , 100) " 
                       + " from Product where " 
                   when @a_intPosition = 5 then                  --商業(yè)機(jī)會(huì)庫(kù)
                         "select ID , Title ,'Description' = left(convert(varchar,content) , 100) "
                       + " from BusinessChance where "
                   when @a_intPosition = 6 then                   --公司庫(kù)
                         "select CompanyID , 'Title' = CompanyName , 'Description' =left(Description , 100) "
                       + " from Company where "
               end

      /*根據(jù)分類ID決定sql的分類部分*/
      select @m_strSqlCategory 
             = case
                   when @a_strCategoryID > "0" then " CategoryID like '" + @a_strCategoryID + "%' and "
                   else ""
               end

      /*根據(jù)調(diào)用位置決定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                   --商業(yè)機(jī)會(huì)  
                        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   

標(biāo)簽:蚌埠 棗莊 江蘇 廣元 萍鄉(xiāng) 衢州 衡水 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《結(jié)合asp和存儲(chǔ)過(guò)程做的搜索程序》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    秀山| 德安县| 霍林郭勒市| 土默特右旗| 黑河市| 城步| 钟山县| 诸城市| 泗阳县| 长顺县| 阿鲁科尔沁旗| 双城市| 宁远县| 宜兰县| 黎川县| 林周县| 阿巴嘎旗| 平果县| 美姑县| 象州县| 青神县| 峨眉山市| 双柏县| 贞丰县| 黑水县| 项城市| 漳平市| 宁津县| 黄骅市| 榆中县| 乐昌市| 庄河市| 泗洪县| 纳雍县| 济源市| 大埔区| 平果县| 裕民县| 同江市| 遂川县| 麻城市|