Consider the following example:
.
declare @String varchar(200) set @string = 'test,test2,test3' select @string
row1: test row2: test2 row3: test3well it turns out it can be done pretty simply. The first step is constructing your delimited list into a roughly formated xml document. You can then use standard xquery syntax to shred that document into its row by row element values. Below is the finished code example:
declare @TableList varchar(100)
set @TableList = 'test,test2,test3'
--show what xml looks like for example purpose only.
select Convert(xml,('<R>'+replace(@TableList,',','</R><R>')+'</R>')) as xmldoc
select t.value('.','varchar(200)')
from (select convert(xml,('<R>'+replace(@TableList,',','</R><R>')+'</R>')) as xmldoc) as a
Cross apply a.xmldoc.nodes('./R') as b(t)
No comments:
Post a Comment