Here's a problem that I usually encounter on the forum and at work. Sometimes I need a stored proc or function to read a table and return a single string delimited with either comma or tab or bar.
Although SQL Server 2005 included a PIVOT/UNPIVOT relational operators, I think it's still worth it to look at this solution.
Consider this table:
declare @myTable table (rownum int, rowname varchar(10)) declare @DelimitedString varchar(max) insert @myTable values (1, 'One') insert @myTable values (2, 'Two') insert @myTable values (3, 'Three') insert @myTable values (4, 'Four') insert @myTable values (5, 'Five') insert @myTable values (6, 'Six') insert @myTable values (7, 'Seven') insert @myTable values (8, 'Eight') insert @myTable values (9, 'Nine') insert @myTable values (10, 'Ten') select * from @mytable rownum rowname ----------- ---------- 1 One 2 Two 3 Three 4 Four 5 Five 6 Six 7 Seven 8 Eight 9 Nine 10 TenI need this output:
One,Two,Three,Four,Five,Six,Seven,Eight,Nine,TenSo far, this is the best code that I got:
declare @DelimitedString varchar(max) SELECT @DelimitedString = COALESCE(@DelimitedString+',' , '') + ColumnName FROM @MyTable SELECT @DelimitedStringHere's the result set:
------------------------------------------------- One,Two,Three,Four,Five,Six,Seven,Eight,Nine,TenThen I tried parsing that string back to rows:
declare @xmldoc int declare @DelimitedString varchar(250) set @DelimitedString = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten' set @DelimitedString = '<root><z><y>' + replace(@DelimitedString, ',', '</y></z><z><y>') + '</y></z></root>' select @DelimitedStringThe variable now looks like this:
<root> <z> <y>One</y> </z> <z> <y>Two</y> </z> <z> <y>Three</y> </z> <z> <y>Four</y> </z> <z> <y>Five</y> </z> <z> <y>Six</y> </z> <z> <y>Seven</y> </z> <z> <y>Eight</y> </z> <z> <y>Nine</y> </z> <z> <y>Ten</y> </z> </root>Followed by...
exec sp_xml_preparedocument @xmldoc OUTPUT, @DelimitedString select y as rows from openxml(@xmldoc, '/root/z',2) with (y varchar(15)) exec sp_xml_removedocument @xmldocHere's the result set:
rows --------------- One Two Three Four Five Six Seven Eight Nine TenAs always, comments are welcome
~~ CK
No comments:
Post a Comment