您的位置:首页 > 其它

分拆字符串

2009-05-22 18:38 190 查看
有一字段值为: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

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: