sql中生成查詢的模糊匹配字符串
更新時(shí)間:2007年03月21日 00:00:00 作者:
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
--為了效率,所以要一個(gè)輔助表配合
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
可以任意指定括號(hào)
生成的條件表達(dá)式為 like 模糊匹配
--鄒建 2004.08(引用請(qǐng)保留此信息)--*/
/*--調(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 --在那個(gè)字段中檢索
)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
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
--為了效率,所以要一個(gè)輔助表配合
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
可以任意指定括號(hào)
生成的條件表達(dá)式為 like 模糊匹配
--鄒建 2004.08(引用請(qǐng)保留此信息)--*/
/*--調(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 --在那個(gè)字段中檢索
)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
相關(guān)文章
SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解
這篇文章主要介紹了SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10SqlServer 基礎(chǔ)知識(shí) 數(shù)據(jù)檢索、查詢排序語句
SqlServer 基礎(chǔ)知識(shí) 數(shù)據(jù)檢索、查詢排序語句,需要的朋友可以參考下。2011-10-10sqlserver中的自定義函數(shù)的方法小結(jié)
“自定義函數(shù)”是我們平常的說法,而“用戶定義的函數(shù)”是 SQL Server 中書面的說法。2010-06-06實(shí)例理解SQL中truncate和delete的區(qū)別
這篇文章主要介紹了實(shí)例理解SQL中truncate和delete的區(qū)別,truncate和delete兩者易混,本文就為大家進(jìn)行區(qū)分兩者的異同,感興趣的小伙伴們可以參考一下2016-02-02淺述SQL Server的聚焦強(qiáng)制索引查詢條件和Columnstore Index
本文主要講了強(qiáng)制使用索引條件來進(jìn)行查詢,當(dāng)對(duì)于使用默認(rèn)創(chuàng)建索引進(jìn)行查詢計(jì)劃時(shí)覺得不是最優(yōu)解,可以嘗試使用強(qiáng)制索引來進(jìn)行對(duì)比找出更好得解決方案。簡(jiǎn)短的內(nèi)容,深入的理解.有興趣的朋友可以看下2016-12-12