分拆字符串

有一字段值爲: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
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章