| Author |
Topic |
|
neophyte226
Starting Member
7 Posts |
Posted - 2009-08-03 : 14:57:30
|
| What am I missing in order to return multiple values for a column off of the record number?DECLARE @h intDECLARE @x xmlset @x = (select cast('<test><date>10-02-2006</date><rec-number>9</rec-number><tranDetail><name>Art</name><name>John</name><name>Todd</name></tranDetail></test>' as xml))EXEC sp_xml_preparedocument @h output, @xselect * from openxml(@h, 'test', 2)with (date varchar(20) 'date',[rec-number] int 'rec-number',[name] varchar(10) 'tranDetail/name')EXEC sp_xml_removedocument @hreturnsdate rec-number name10-02-2006 9 Artbut i'm looking fordate rec-number name10-02-2006 9 Art10-02-2006 9 John10-02-2006 9 Todd |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-03 : 15:02:59
|
Are you using SQL Server 2000 or SQL Server 2005? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
neophyte226
Starting Member
7 Posts |
Posted - 2009-08-03 : 15:24:34
|
| Thanks for the interest and it's SQL Server 2005. I have a data dump in xml format I need to parse and maybe I should be using xml schema? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-03 : 15:37:23
|
[code]DECLARE @x xmlset @x = '<test><date>10-02-2006</date><rec-number>9</rec-number><tranDetail><name>Art</name><name>John</name><name>Todd</name></tranDetail></test>'SELECT t.n.value('date[1]', 'datetime') AS [date], t.n.value('rec-number[1]', 'varchar(12)') AS [rec-number], n.n.value('.', 'varchar(20)') AS nameFROM @x.nodes('//test') AS t(n)cross apply t.n.nodes('tranDetail/name') AS n(n)code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
neophyte226
Starting Member
7 Posts |
Posted - 2009-08-03 : 16:37:12
|
| Thanks but I'm still having trouble understanding the pivot record vs the attributes. Below is a sample of the schema I'm working with and I get lost as the the schema gets more tags.DECLARE @h int --9272 recordsDECLARE @x xmlset @x = '<xml><records> <record> <rec-number>2</rec-number> <contributors> <authors> <author>Adam</author> <author>Barry</author> <author>Cary</author> </authors> </contributors> </record> <record> <rec-number>6</rec-number> <contributors> <authors> <author>Adam</author> <author>Eric</author> <author>John</author> </authors> </contributors> </record> <record> <rec-number>8</rec-number> <contributors> <authors> <author></author> </authors> </contributors> </record></records></xml>'select t.n.value('rec-number[1]', 'int') as [rec-number], n.n.value('.', 'varchar(50)') as authorfrom @x.nodes('/xml/records/record') as t(n) cross apply t.n.nodes('//author') as n(n)order by 1, 2 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-04 : 00:38:19
|
[code]DECLARE @h int --9272 recordsDECLARE @x xmlset @x = '<xml> <records> <record> <rec-number>2</rec-number> <contributors> <authors> <author>Adam</author> <author>Barry</author> <author>Cary</author> </authors> </contributors> </record> <record> <rec-number>6</rec-number> <contributors> <authors> <author>Adam</author> <author>Eric</author> <author>John</author> </authors> </contributors> </record> <record> <rec-number>8</rec-number> <contributors> <authors> <author></author> </authors> </contributors> </record> </records></xml>'select t.n.value('rec-number[1]', 'int') as [rec-number] , n.n.value('.', 'varchar(50)') as authorfrom @x.nodes('//xml/records/record') as t(n)cross apply t.n.nodes('contributors/authors/author') as n(n)order by 1, 2[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
neophyte226
Starting Member
7 Posts |
Posted - 2009-08-04 : 09:28:34
|
| Thank you very much. I guess I need to do some more reading on XML in SQL server. |
 |
|
|
weipublic
Starting Member
19 Posts |
Posted - 2009-08-06 : 01:31:53
|
Hi neophyte226, following should work for your original requirements.select * from openxml(@h, '//name', 2)with (date varchar(20) '../../date', [rec-number] int '../../rec-number', [name] varchar(10) 'text()' ) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-06 : 02:25:12
|
Since OP is using SQL Server 2005, he better use the new ways to deal with XML data. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
weipublic
Starting Member
19 Posts |
Posted - 2009-08-08 : 13:04:31
|
| Thanks Peso!!! |
 |
|
|
|