问题:职位表Job_OfficeList的GWCode保存的数据格式 24008,29001,32344
职位订阅表保存的查询条件是 24008,31323
要查询该条件的数据:实现方法:
一:自定义分割函数:
Create function [F_Split](@c varchar(4000) , @split varchar(2))
returns @t table(col varchar(256))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
二:判断用逗号隔开的变量是否存在集合中
create function [dbo].[checkStringTostring](@datas varchar(200),@values varchar(200))
returns bit
as
begin
declare @tmpcount int
set @tmpcount=0
declare @tmpstr varchar(50)
declare mycur cursor for
select col from dbo.f_split(@datas,',')
open mycur
fetch next from mycur into @tmpstr
WHILE @@FETCH_STATUS = 0
begin
if len(@tmpstr)=4
begin
select @tmpcount=count(*) from dbo.f_split(@values,',') where substring(col,1,4)=@tmpstr
if(@tmpcount>0)
begin
CLOSE mycur
DEALLOCATE mycur
return 1
end
end
else
begin
select @tmpcount=count(*) from dbo.f_split(@values,',') where col=@tmpstr
if(@tmpcount>0)
begin
CLOSE mycur
DEALLOCATE mycur
return 1
end
end
fetch next from mycur into @tmpstr
end
declare mycur2 cursor for
select col from dbo.f_split(@values,',') where len(col)=4
open mycur2
fetch next from mycur2 into @tmpstr
WHILE @@FETCH_STATUS = 0
begin
select @tmpcount=count(*) from dbo.f_split(@datas,',') where substring(col,1,4)=@tmpstr
if(@tmpcount>0)
begin
CLOSE mycur
DEALLOCATE mycur
CLOSE mycur2
DEALLOCATE mycur2
return 1
end
fetch next from mycur2 into @tmpstr
end
CLOSE mycur
DEALLOCATE mycur
CLOSE mycur2
DEALLOCATE mycur2
return 0;
end
三。用SQL语句实现:
select * from job_officelist where dbo.checkStringTostring(GWCode,'24008,22008')=1