要实现join字符串
select * FROM table1 as t1
right join (select '1,2,3,4,5' as t) as tt on t1.Id=tt.t
则需要分割字符串为数组,以下为实现的分割字符串函数split
split函数及使用示例:
select * FROM table1 as t1
right join (select * from split('1,3,5',',')) as tt on t1.Id=tt.F1
split函数:
/*
获取字符串数组的 Table www.cnblogs.com/xqhppt/p/4377757.html
*/
if exists (select 1 from sysobjects where id = object_id('split' ))
drop Function split
go
CREATE function split(
@SourceSql varchar (max),
@StrSeprate varchar (10)
)
returns @temp table( F1 varchar (100))
as
begin
declare @i int
set @SourceSql =rtrim( ltrim(@SourceSql ))
set @i =charindex( @StrSeprate,@SourceSql )
while @i >=1
begin
insert @temp values(left( @SourceSql,@i -1))
set @SourceSql =substring( @SourceSql,@i +1, len(@SourceSql )-@i)
set @i =charindex( @StrSeprate,@SourceSql )
end
if @SourceSql <>''
insert @temp values( @SourceSql)
return
end
GO
FromUrl:www.cnblogs.com/xuejianxiyang/p/7025929.html