加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在sql中生成查询的模糊匹配字符串

发布时间:2023-08-11 15:01:18 所属栏目:MsSql教程 来源:
导读:ifexists(selectfromdbo.sysobjectswhereid=objectid(N'[dbo].[fsql]')andxtypein(N'FN',N'IF',N'TF'))

dropfunction[dbo].[f_sql]

GO ifexists(selectfromdbo.sysobjectsw
ifexists(selectfromdbo.sysobjectswhereid=objectid(N'[dbo].[fsql]')andxtypein(N'FN',N'IF',N'TF'))
 
dropfunction[dbo].[f_sql]
 
GO ifexists(selectfromdbo.sysobjectswhereid=object_id(N'[序数表]')andobJECTPROPERTY(id,N'IsUserTable')=1)
 
droptable[序数表]
 
GO --为了效率,所以要一个辅助表配合
 
selecttop1000id=identity(int,1,1)into序数表
 
fromsyscolumnsa,syscolumnsb
 
altertable序数表addconstraintpk_id_序数表primarykey(id)
 
go /--根据指定字符串生成查询的模糊匹配字符串 条件连接的关键字为and,or
 
可以任意指定括号
 
生成的条件表达式为like模糊匹配 --邹建2004.08(引用请保留此信息)--/ /--调用示例 --调用示例
 
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')
 
--/
 
--示例函数
 
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)-id)+''
 
whensubstring(@str,1)='('
 
then'(['+@fdname+']like''%'
 
+substring(@str,id+1,id)-id-1)
 
+'%'''
 
whensubstring(@str,id)-1,1)=')'
 
then'['+@fdname+']like''%'
 
+substring(@str,id)-id-1)
 
+'%'')'
 
else'['+@fdname+']like''%'
 
+substring(@str,id)-id)
 
+'%'''
 
end
 
from序数表
 
whereid<=len(@str)
 
andcharindex('',''+@str,id)-id=0
 
return(@r)
 
end
 
go
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章