Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 OPENXML relational data query

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 int
DECLARE @x xml
set @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, @x

select * from openxml(@h, 'test', 2)
with (date varchar(20) 'date',
[rec-number] int 'rec-number',
[name] varchar(10) 'tranDetail/name'
)
EXEC sp_xml_removedocument @h

returns
date rec-number name
10-02-2006 9 Art

but i'm looking for
date rec-number name
10-02-2006 9 Art
10-02-2006 9 John
10-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"
Go to Top of Page

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?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-03 : 15:37:23
[code]DECLARE @x xml

set @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 name
FROM @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"
Go to Top of Page

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 records
DECLARE @x xml
set @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 author
from
@x.nodes('/xml/records/record') as t(n)
cross apply t.n.nodes('//author') as n(n)
order by 1, 2
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-04 : 00:38:19
[code]DECLARE @h int --9272 records
DECLARE @x xml
set @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 author
from @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"
Go to Top of Page

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.
Go to Top of Page

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()'
)
Go to Top of Page

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"
Go to Top of Page

weipublic
Starting Member

19 Posts

Posted - 2009-08-08 : 13:04:31
Thanks Peso!!!
Go to Top of Page
   

- Advertisement -