sql中生成查詢的模糊匹配字符串
發(fā)布日期:2022-02-04 09:44 | 文章來源:gibhub
dropfunction[dbo].[f_Sql]
GO ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[序數(shù)表]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[序數(shù)表]
GO --為了效率,所以要一個輔助表配合
selecttop1000id=identity(int,1,1)into序數(shù)表
fromsyscolumnsa,syscolumnsb
altertable序數(shù)表addconstraintpk_id_序數(shù)表primarykey(id)
go /*--根據(jù)指定字符串生成查詢的模糊匹配字符串 條件連接的關(guān)鍵字為and,or
可以任意指定括號
生成的條件表達式為like模糊匹配 --鄒建2004.08(引用請保留此信息)--*/ /*--調(diào)用示例 --調(diào)用示例
selectA=dbo.f_Sql('(WeborHTMLorInternet)and(ProgrammerorDeveloper)','content')
selectB=dbo.f_Sql('WeborHTMLorInternet','content')
selectC=dbo.f_Sql('(WebandHTML)','content')
selectD=dbo.f_Sql('Web','content')
--*/
--示例函數(shù)
createfunctionf_Sql(
@strNvarchar(1000),--要檢索的字符串
@fdnamesysname--在那個字段中檢索
)returnsNvarchar(4000)
as
begin
declare@rNvarchar(4000)
set@r=''
select@r=@r+case
whensubstring(@str,id,charindex('',@str+'',id)-id)in('or','and')
then''+substring(@str,id,charindex('',@str+'',id)-id)+''
whensubstring(@str,id,1)='('
then'(['+@fdname+']like''%'
+substring(@str,id+1,charindex('',@str+'',id)-id-1)
+'%'''
whensubstring(@str,charindex('',@str+'',id)-1,1)=')'
then'['+@fdname+']like''%'
+substring(@str,id,charindex('',@str+'',id)-id-1)
+'%'')'
else'['+@fdname+']like''%'
+substring(@str,id,charindex('',@str+'',id)-id)
+'%'''
end
from序數(shù)表
whereid<=len(@str)
andcharindex('',''+@str,id)-id=0
return(@r)
end
go
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信