--创建处理函数 create function f_comp(@a varchar(8000),@b varchar(8000)) returns bit as begin declare @r bit,@i int,@s varchar(8000) if len(@a)>len(@b) select @s=@a,@a=@b,@b=@s select @b=' '+@b+' ',@i=charindex(' ',@a),@r=0 while @i>0 and @r=0 select @r=case when @b like '%'+left(@a,@i-1)+'%' then 1 else 0 end ,@a=substring(@a,@i+1,8000) ,@i=charindex(' ',@a) if @r=0 and len(@a)>0 set @r=case when @b like '% '+@a+' %' then 1 else 0 end return(@r) end go--查询 select b.* from A表 a,B表 b where a.id=1 and a.dept='001' and dbo.f_comp(a.DEST,b.AREA)=1
--测试--测试数据 create table A表(ID int, DEPT char(3),NAME varchar(10),TITLE varchar(10),AGE int,DEST varchar(50)) insert A表 select 1,'001','LEE','WORKER',50,'北京 武汉 天津' union all select 2,'002','WU','WORKER',35,'包头 呼和浩特 ' union all select 3,'003','KAI','MANAGER',25,'西安 荆州'create table B表(ID char(3),NAME varchar(10),CODE char(3),AREA varchar(50)) insert B表 select '001','AAA','000','天津 呼和浩特 武汉' union all select '002','BBB','004','杭州 上海' union all select '003','CCC','001','长春 荆州 天津' union all select '004','DDD','003','武汉 包头 西安' union all select '005','EEE','002','香港 深圳 广州' union all select '006','FFF','005','北京 武汉 南京' go--创建处理函数 create function f_comp(@a varchar(8000),@b varchar(8000)) returns bit as begin declare @r bit,@i int,@s varchar(8000) if len(@a)>len(@b) select @s=@a,@a=@b,@b=@s select @b=' '+@b+' ',@i=charindex(' ',@a),@r=0 while @i>0 and @r=0 select @r=case when @b like '%'+left(@a,@i-1)+'%' then 1 else 0 end ,@a=substring(@a,@i+1,8000) ,@i=charindex(' ',@a) if @r=0 and len(@a)>0 set @r=case when @b like '% '+@a+' %' then 1 else 0 end return(@r) end go--查询 select b.* from A表 a,B表 b where a.id=1 and a.dept='001' and dbo.f_comp(a.DEST,b.AREA)=1 go--删除测试 drop table A表,B表 drop function f_comp/*--测试结果 ID NAME CODE AREA ---- ---------- ---- ------------------------- 001 AAA 000 天津 呼和浩特 武汉 003 CCC 001 长春 荆州 天津 004 DDD 003 武汉 包头 西安 006 FFF 005 北京 武汉 南京(所影响的行数为 4 行) --*/
create function f_comp(@a varchar(8000),@b varchar(8000))
returns bit
as
begin
declare @r bit,@i int,@s varchar(8000)
if len(@a)>len(@b) select @s=@a,@a=@b,@b=@s
select @b=' '+@b+' ',@i=charindex(' ',@a),@r=0
while @i>0 and @r=0
select @r=case when @b like '%'+left(@a,@i-1)+'%'
then 1 else 0 end
,@a=substring(@a,@i+1,8000)
,@i=charindex(' ',@a)
if @r=0 and len(@a)>0
set @r=case when @b like '% '+@a+' %' then 1 else 0 end
return(@r)
end
go--查询
select b.*
from A表 a,B表 b
where a.id=1 and a.dept='001'
and dbo.f_comp(a.DEST,b.AREA)=1
create table A表(ID int, DEPT char(3),NAME varchar(10),TITLE varchar(10),AGE int,DEST varchar(50))
insert A表 select 1,'001','LEE','WORKER',50,'北京 武汉 天津'
union all select 2,'002','WU','WORKER',35,'包头 呼和浩特 '
union all select 3,'003','KAI','MANAGER',25,'西安 荆州'create table B表(ID char(3),NAME varchar(10),CODE char(3),AREA varchar(50))
insert B表 select '001','AAA','000','天津 呼和浩特 武汉'
union all select '002','BBB','004','杭州 上海'
union all select '003','CCC','001','长春 荆州 天津'
union all select '004','DDD','003','武汉 包头 西安'
union all select '005','EEE','002','香港 深圳 广州'
union all select '006','FFF','005','北京 武汉 南京'
go--创建处理函数
create function f_comp(@a varchar(8000),@b varchar(8000))
returns bit
as
begin
declare @r bit,@i int,@s varchar(8000)
if len(@a)>len(@b) select @s=@a,@a=@b,@b=@s
select @b=' '+@b+' ',@i=charindex(' ',@a),@r=0
while @i>0 and @r=0
select @r=case when @b like '%'+left(@a,@i-1)+'%'
then 1 else 0 end
,@a=substring(@a,@i+1,8000)
,@i=charindex(' ',@a)
if @r=0 and len(@a)>0
set @r=case when @b like '% '+@a+' %' then 1 else 0 end
return(@r)
end
go--查询
select b.*
from A表 a,B表 b
where a.id=1 and a.dept='001'
and dbo.f_comp(a.DEST,b.AREA)=1
go--删除测试
drop table A表,B表
drop function f_comp/*--测试结果
ID NAME CODE AREA
---- ---------- ---- -------------------------
001 AAA 000 天津 呼和浩特 武汉
003 CCC 001 长春 荆州 天津
004 DDD 003 武汉 包头 西安
006 FFF 005 北京 武汉 南京(所影响的行数为 4 行)
--*/