February 4, 2013 at 12:12 pm
From the sample here:
http://blogs.msdn.com/b/simonince/archive/2009/04/24/flattening-xml-data-in-sql-server.aspx
How would I sum the Ages of Simon and Peter?
CREATE TABLE XmlSourceTable
(
RecordId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
XmlData XML NOT NULL
)
GO
XmlData = '
<?xml version="1.0" ?>
<Root>
<Person>
<Name>Simon</Name>
<Age>20</Age>
<Skills>
<Skill>Cooking</Skill>
<Skill>Cleaning</Skill>
</Skills>
</Person>
<Person>
<Name>Peter</Name>
<Age>21</Age>
<Skills>
<Skill>Ironing</Skill>
</Skills>
</Person>
</Root>'
INSERT INTO XmlSourceTable(XmlData)
SELECT *
FROM OPENROWSET(
BULK 'C:\XmlSource.xml', SINGLE_BLOB)
AS ImportSource
GO
SELECT
pref.value('(Name/text())[1]', 'varchar(50)') as PersonName,
pref.value('(Age/text())[1]', 'int') as PersonAge,
pref.query('Skills') as PersonSkills
FROM
XmlSourceTable CROSS APPLY
XmlData.nodes('/Root/Person') AS People(pref)
GO
February 4, 2013 at 6:01 pm
Not sure exactly if this is what you're after but maybe it'll help:
DECLARE @XmlData XML = '
<Root>
<Person>
<Name>Simon</Name>
<Age>20</Age>
<Skills>
<Skill>Cooking</Skill>
<Skill>Cleaning</Skill>
</Skills>
</Person>
<Person>
<Name>Peter</Name>
<Age>21</Age>
<Skills>
<Skill>Ironing</Skill>
</Skills>
</Person>
</Root>'
SELECT [Simon's Age]=x.value('(/Root/Person/Age)[1]', 'INT')
,[Peter's Age]=x.value('(/Root/Person/Age)[2]', 'INT')
FROM (SELECT @XmlData) a(x)
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy