sql中生成查詢的模糊匹配字符串
更新時間: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
--為了效率,所以要一個輔助表配合
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ù)指定字符串生成查詢的模糊匹配字符串
條件連接的關鍵字為 and,or
可以任意指定括號
生成的條件表達式為 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
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ù)指定字符串生成查詢的模糊匹配字符串
條件連接的關鍵字為 and,or
可以任意指定括號
生成的條件表達式為 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
相關文章
將表數(shù)據(jù)生成Insert腳本 比較好用的生成插入語句的SQL腳本
比較好用的生成插入語句的SQL腳本 將表數(shù)據(jù)生成Insert腳本2010-05-05
SQLServer中用T—SQL命令查詢一個數(shù)據(jù)庫中有哪些表的sql語句
SQLServer如何用T—SQL命令查詢一個數(shù)據(jù)庫中有哪些表,方便進行表操作,需要的朋友可以參考下2012-06-06
數(shù)據(jù)庫中聚簇索引與非聚簇索引的區(qū)別[圖文]
在《數(shù)據(jù)庫原理》里面,對聚簇索引的解釋是:聚簇索引的順序就是數(shù)據(jù)的物理存儲順序,而對非聚簇索引的解釋是:索引順序與數(shù)據(jù)物理排列順序無關。正式因為如此,所以一個表最多只能有一個聚簇索引2012-02-02
SQLServer性能優(yōu)化--間接實現(xiàn)函數(shù)索引或者Hash索引
本文主要介紹了SQLServer性能優(yōu)化--間接實現(xiàn)函數(shù)索引或者Hash索引的解決方式。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
SQLSERVER Pager store procedure分頁存儲過程
SQL SERVER(2005)以上版本可用,相對應的頁面邏輯中寫的對應調(diào)用該存儲過程的方法2010-06-06

