日期:2014-05-17 浏览次数:20572 次
select * from tb
where charindex(','+cast(id as varchar)+',',','+字段+',')>0
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [name] varchar(100));
insert #temp
select '1,2,3' union all
select '21,32' union all
select '13,24,32'
--SQL:
SELECT a.rowid, b.[name]
FROM
(SELECT rowid=ROW_NUMBER() OVER(ORDER BY GETDATE()),[name]=CONVERT(XML, '<root><v>'+replace([name],',','</v><v>')+'</v></root>') FROM #temp) a
OUTER APPLY
(SELECT [name] = C.v.value('.','NVARCHAR(MAX)') FROM a.[name].nodes('/root/v') C(v)) b
/*
rowid name
1 1
1 2
1 3
2 21
2 32
3 13
3 24
3 32
*/
select * from 另外一个表 t1
where exists(select 1 from tb
where charindex(','+cast(id as varchar)+',',','+字段+',')>0)
select a.*
from ta a
inner join tb b on charindex(','+cast(a.id as varchar)+',',','+b.字段+',')>0