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

首頁 > 數據庫 > SQL Server > 正文

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

2024-08-31 01:00:12
字體:
來源:轉載
供稿:網友
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'[序數表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序數表]
GO

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

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

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

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

/*--調用示例

 --調用示例
 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')
--*/
--示例函數
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 序數表
 where id<=len(@str)
  and charindex(' ',' '+@str,id)-id=0
 return(@r)
end
go

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃园市| 中宁县| 扎囊县| 错那县| 西丰县| 合川市| 皋兰县| 宜兰县| 眉山市| 宜州市| 哈密市| 什邡市| 平阳县| 双牌县| 天津市| 景谷| 彰武县| 龙州县| 武冈市| 海口市| 华宁县| 三穗县| 射洪县| 昌黎县| 民丰县| 海伦市| 玉屏| 宁乡县| 苍梧县| 沁源县| 玉环县| 贵州省| 千阳县| 平塘县| 衡阳市| 开原市| 武汉市| 关岭| 胶南市| 中宁县| 安平县|