国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

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

2020-07-25 13:39:27
字體:
供稿:網(wǎng)友
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_Sql]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_Sql]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[序數(shù)表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序數(shù)表]
GO

--為了效率,所以要一個輔助表配合
select top 1000 id=identity(int,1,1) into 序數(shù)表 
from syscolumns a,syscolumns b
alter table 序數(shù)表 add constraint pk_id_序數(shù)表 primary key(id)
go

/*--根據(jù)指定字符串生成查詢的模糊匹配字符串

 條件連接的關(guān)鍵字為 and,or
 可以任意指定括號
 生成的條件表達(dá)式為 like 模糊匹配

--鄒建 2004.08(引用請保留此信息)--*/

/*--調(diào)用示例

 --調(diào)用示例
 select A=dbo.f_Sql('(Web or HTML or Internet) and (Programmer or Developer)','content')
 select B=dbo.f_Sql('Web or HTML or Internet','content')
 select C=dbo.f_Sql('(Web and HTML)','content')
 select D=dbo.f_Sql('Web','content')
--*/
--示例函數(shù)
create function f_Sql(
@str Nvarchar(1000), --要檢索的字符串
@fdname sysname --在那個字段中檢索
)returns Nvarchar(4000)
as
begin
 declare @r Nvarchar(4000)
 set @r=''
 select @r=@r+case
  when substring(@str,id,charindex(' ',@str+' ',id)-id) in('or','and')
   then ' '+substring(@str,id,charindex(' ',@str+' ',id)-id)+' '
  when substring(@str,id,1)='('
   then '(['+@fdname+'] like ''%'
    +substring(@str,id+1,charindex(' ',@str+' ',id)-id-1)
    +'%'''
  when substring(@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ù)表
 where id<=len(@str)
  and charindex(' ',' '+@str,id)-id=0
 return(@r)
end
go

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长丰县| 广安市| 鄯善县| 开封市| 谢通门县| 旬阳县| 石家庄市| 衡阳市| 安平县| 扶风县| 饶阳县| 库车县| 临潭县| 瓮安县| 高台县| 喜德县| 平利县| 固镇县| 龙里县| 观塘区| 腾冲县| 平度市| 南投县| 潞城市| 台安县| 邓州市| 江达县| 镇原县| 浪卡子县| 格尔木市| 阿合奇县| 江安县| 凤台县| 芜湖市| 恩施市| 太仆寺旗| 清徐县| 清徐县| 论坛| 喀喇沁旗| 泰安市|