I've almost got what I want after shredding up some serious Xml--but after looking at the results, I see that in one section of the parsing, I can't easily resolve this pattern of iterating through all of the line details for each of the subheaders-- so instead of writing out a total of let's say 3 records for all of the line items, I'm writing out three line items for each of the subs--of which let's say I have two. I wind up with a total of 6! :-( I've distilled the pattern as a generic header/subheader/detail relationship model in the code that follows.
DECLARE @MetroXML xml
SET @MetroXML =
'<Header>
<col1>Conoco</col1>
<col2>ORD-1111</col2>
<SubHeaders>
<SubHeader>
<col1>Dallas</col1>
<col2>BOL-2213</col2>
<Details>
<Detail>
<col1>Diesel</col1>
<col2>7600.00</col2>
</Detail>
</Details>
</SubHeader>
</SubHeaders>
<SubHeaders>
<SubHeader>
<col1>Fort Worth</col1>
<col2>BOL-2216</col2>
<Details>
<Detail>
<col1>Ethanol</col1>
<col2>1852.00</col2>
</Detail>
<Detail>
<col1>Unleaded</col1>
<col2>900.00</col2>
</Detail>
</Details>
</SubHeader>
</SubHeaders>
</Header>';
INSERT INTO [scratch].GenericHeader
SELECT T.c.value('col1[1]','varchar(10)') AS 'col1',
T.c.value('col2[1]','varchar(10)') AS 'col2'
FROM @MetroXML.nodes('/Header') T(c);
INSERT [scratch].GenericSubHeader
(id,col1,col2)
SELECT
h.id,
n.x.value('col1[1]','varchar(10)') AS 'col1',
n.x.value('col2[1]','varchar(10)') AS 'col2'
FROM [scratch].GenericHeader h
CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader') n(x);
INSERT [scratch].GenericDetail
(id,subid,col1,col2)
SELECT
s.id,
s.subid,
n.x.value('col1[1]','varchar(10)') AS 'col1',
n.x.value('col2[1]','varchar(10)') AS 'col2'
FROM [scratch].GenericSubHeader s
CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader/Details/Detail') as n(x);
select * from [scratch].GenericHeader
where id = 24;
select * from [scratch].GenericSubHeader
where id = 24;
select * from [scratch].GenericDetail
where id = 24;
NOTE: id,subid,detid are defined as INT IDENTITY(1,1) Results
What I get:
id|subid|detid|col1 |col2
--------------------------------
24|44 |22 |Diesel |7600.00
24|44 |23 |Ethanol |1852.00
24|44 |24 |Unleaded |900.00
24|48 |25 |Diesel |7600.00
24|48 |26 |Ethanol |1852.00
24|48 |27 |Unleaded |900.00
What I want to get:
id|subid|detid|col1 |col2
--------------------------------
24|44 |22 |Diesel |7600.00
24|48 |23 |Ethanol |1852.00
24|48 |24 |Unleaded |900.00