SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML View?


XML View?

Author
Message
al_nick
al_nick
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 86
If i have at table with a xml column datatype and want to create sql view which displays certain values from within xml nodes of ALL the table records (not just one) what is the best way to do this? All the examples I've seen take simply the value of one xml column of one records a produce a reuslt but what if I wanted to query over all the records and produce SUMS, group bys, ETC. Is the only way for me to creatre a sproc and loop through every record, insert them into a temp table, and then do my selects/aggregations? Or is there a better more efficient way?

thanks for any advice!
nick



s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1865 Visits: 2292
Hi,

If you look at the details of this post it may help you. If I understand what you want....

http://www.sqlservercentral.com/Forums/Topic1438775-3077-1.aspx

Thanks,


Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1865 Visits: 2292
Or.... here's a really simple example but should get you going....

/* Set up a table variable to hold the XML*/
DECLARE @table TABLE
(
RiskXML XML
)

/* Insert some very simple XML */
INSERT INTO @table
SELECT '<RISK>
<ID>1</ID>
<VALUE>20</VALUE>
</RISK>'
UNION ALL SELECT '<RISK>
<ID>2</ID>
<VALUE>20</VALUE>
</RISK>'
UNION ALL SELECT '<RISK>
<ID>3</ID>
<VALUE>20</VALUE>
</RISK>'

/* Prove it's in there! */
SELECT * FROM @table


/* This is how we can flatten (shred) it */
SELECT
r.risk.value('(ID)[1]', 'int') AS ID,
r.risk.value('(VALUE)[1]', 'int') AS Value
FROM
@table t
CROSS APPLY t.RiskXML.nodes('RISK') r(risk)

/* Simple aggregations on it */
SELECT
COUNT(r.risk.value('(ID)[1]', 'int')) AS [Count],
SUM(r.risk.value('(VALUE)[1]', 'int')) AS [Sum]
FROM
@table t
CROSS APPLY t.RiskXML.nodes('RISK') r(risk)





MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
al_nick
al_nick
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 86
Thanks Simon! Much obliged. Think you gave me enough to get started.



s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1865 Visits: 2292
Cool, glad it's helped. Post back if you have any problems.

Thanks,


Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search