SQL: shread xml to table where attributes not known a priori

Folks,

lets say that I have the following table.
Name   XmlData
-------   ---------
bob     <root><Column name="haircolor">blue</Column><Column name="title">Boss</Column></root>
jane    <root><Column name="haircolor">green</Column><Column name="title">peon</Column></root>

Lets also say that a priori I don't know the actual values of the "name" attribute.  Here I show "haircolor" and "title" but it could be others.

what SQL query will convert this into a table:
Name   haircolor     title
-------   ----------   -------
bob       blue           boss
jane       green        peon

again, I don't know which name attributes will exist.  I DO know that the same named attributes will exist in the table.  So if bob has "haircolor" and "title", then everyone else will have the same attributes.
LVL 1
sfun28Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
Hi,

This will help you
/* sample table
create table t3 (
name varchar(5),
xmldata xml);
insert t3 (name, xmldata) values
('bob','<root><Column name="haircolor">blue</Column><Column name="title">Boss</Column></root>');
insert t3 (name, xmldata) values
('jane','<root><Column name="haircolor">green</Column><Column name="title">peon</Column></root>');
*/

declare @xml xml
select top 1 @xml = xmldata from t3
select @xml
declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + ',xmldata.value(''(/root/Column[@name='''''+ names.n.value('.','varchar(100)') +
'''''])[1]'', ''varchar(100)'') '+names.n.value('.','varchar(100)')
from @xml.nodes('//@name') names(n)
set @sql = 'select Name ' + @sql + ' from t3';
--print @sql
exec (@sql)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sfun28Author Commented:
WOW!  perfect.  Its rare on EE to get a perfect code-sample the first time.  I'm looking forward to studying your solution to understand it better!
0
sfun28Author Commented:
PERFECT!!!  thanks so much!!!
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

sfun28Author Commented:
Not to push my luck here, but is there a solution that would work if the attributes aren't guaranteed to exist for all rows?  So if Jane had <Column name="age">10</Column> but Bob doesn't?

Should I post another question for this?
0
cyberkiwiCommented:
Hi there,

This should work

Cheers
declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + ',xmldata.value(''(/root/Column[@name='''''+ nodename +
'''''])[1]'', ''varchar(100)'') '+nodename
from
(
	select distinct nodename = names.n.value('.','varchar(100)')
	from t3 cross apply xmldata.nodes('//@name') names(n)
) X
set @sql = 'select Name ' + @sql + ' from t3';
--print @sql
exec (@sql)

Open in new window

0
sfun28Author Commented:
PERFECT!!!  Thanks so much!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.