select * FROM table1 as t1
right join (select '1,2,3,4,5'  as t) as tt on t1.Id=tt.t

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

05-11 11:04
查看更多