SQLServer字符串批量拆分得方法

其实拆分和批量拆分的方法是一样的,所谓批量就是用out apply来调用单个拆分函数。

/*
SQLServer字符串拆分函数,by jinjazz

--原始数据
id names
----------- --------------------
1 jinjazz,blog,csdn
2 sql,ms

--生成的数据
id rn name
----------- ----------- ----------
1 1 jinjazz
1 2 blog
1 3 csdn
2 1 sql
2 2 ms


*/

setnocounton
usetempdb
go
if (object_id ('f_test' ) isnotnull )
dropfunctionf_test
go
createfunctionf_test (@avarchar (max ))
returns @ttable (rnint , vvarchar (max ))
as
begin
insertinto @t
select b. *from (
selectconvert (xml , '<v>' + replace (@a , ',' , '</v><v>' )+ '</v>' ) asf )a
outerapply
(
SELECTrn = row_number ()over (orderbygetdate ()),t . c . value ('.' , 'varchar(max)' ) ASf
FROMa . f . nodes ('//v' ) ASt (c )
)b
return
end
go


declare @ttable (idint , namesvarchar (20 ))
insertinto @tselect1 , 'jinjazz,blog,csdn'
insertinto @tselect2 , 'sql,ms'

select*from @t
selecta . id , rn , b. vasnamefrom @taouterapplydbo . f_test (a . names ) b

setnocountoff


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/jinjazz/archive/2009/08/27/4490912.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值