有一字段值爲:a,b,c ,想要得到如下:
id name
1 a
2 b
3 c
在SQL中如何實現呢?
-----------------------------------------------
declare @str varchar(8000)
set @str = 'a1,b1,c2,d1,e3,f5'
set @str = 'select name='''+replace(@str,',',''' union all select ''')+'
'''
--如果要編號的話,那就這樣。
--set @str='select id=identity(int,1,1),name into #temp from ('+@str+') a
--select * from #temp drop table #temp'
exec(@str)
/*
name
----
a1
b1
c2
d1
e3
f5
*/