Question

Mal_p10 on Wed, 23 Aug 2006 12:43:03


This is very urgent..Please HELP!!!

Iam in a project which is planning to store the entire XML file in the database in a XML datatype column. The XML has multiple parents AND multiple children for each of those parents. My problem is how to read/store the data for each of the parent and associate the parent to the child elements. The child elements does not have a parentID in its column. Here is an example- Parent table - SHIPMENT. Child Table - SHIPMENTUNITS. ShipmentNBr is the business key in both these tables. This column comes in the SHIPMENT node but does not come in the SHIPMENTUNITS node. When I try the OPENXML, all the data just gets stored..how do I link the child to the parent?

<SHIPMENT xmlns="">

<SHIPMENTNBR>US1011<SHIPMENTNBR />

<SHIPMENTTYPE_CD>BK</SHIPMENTTYPE_CD>

<LINESCAC>ACLH</LINESCAC>

<TERMINAL_CD>USJAX</TERMINAL_CD>

<ISSUE_DT>2006-08-17T14:23:09</ISSUE_DT>

<VESSEL_CD>VES</VESSEL_CD>

<VOYAGE_CD>VOYA</VOYAGE_CD>

<POL />

<POD>POD_0</POD>

<SHIPMENTUNITS>

<UNITNBR>1234</UNITNBR>

<UNITTYPE_CD>CAR</UNITTYPE_CD>

<MAKE>HON</MAKE>

<MODEL>ACCO</MODEL>

<COLOR>RED</COLOR>

<FULLEMPTY_CD>F</FULLEMPTY_CD>

<LENGTH>0011</LENGTH>

<WIDTH>0022</WIDTH>

<HEIGHT>0033</HEIGHT>

<DIMENSIONUOM>C</DIMENSIONUOM>

<WEIGHT>000010</WEIGHT>

<WGTUOM>K</WGTUOM>

</SHIPMENTUNITS>

</SHIPMENT>

<SHIPMENT xmlns="">

<SHIPMENTNBR>IN2052<SHIPMENTNBR />

<SHIPMENTTYPE_CD>BK</SHIPMENTTYPE_CD>

<LINESCAC>ACLH</LINESCAC>

<TERMINAL_CD>USJAX</TERMINAL_CD>

<ISSUE_DT>2006-08-17T14:23:09</ISSUE_DT>

<VESSEL_CD>VES</VESSEL_CD>

<VOYAGE_CD>VOYA</VOYAGE_CD>

<POL />

<POD>POD_0</POD>

<SHIPMENTUNITS>

<UNITNBR>1234</UNITNBR>

<UNITTYPE_CD>CAR</UNITTYPE_CD>

<MAKE>HON</MAKE>

<MODEL>ACCO</MODEL>

<COLOR>RED</COLOR>

<FULLEMPTY_CD>F</FULLEMPTY_CD>

<LENGTH>0011</LENGTH>

<WIDTH>0022</WIDTH>

<HEIGHT>0033</HEIGHT>

<DIMENSIONUOM>C</DIMENSIONUOM>

<WEIGHT>000010</WEIGHT>

<WGTUOM>K</WGTUOM>

</SHIPMENTUNITS>

</SHIPMENT>

 


Sponsored



Replies

MRys on Sun, 03 Sep 2006 02:48:19


I don't quite understand what you want to achieve.

If you store the data in an XML data type then you get the XML as is and all the children are associated with their parents.

If you want to decompose them into relational tables using OpenXML, then you either use the id values provided in the data and use the parent axis, or if no such id values are present look at the so called meta properties @mp:id and @mp:parentid. In your case you have the shipment number and unit number that you could use. The first approach you can do with the nodes() method too (but the meta properties are OpenXML only).

In the following example I show you both. In your case you don't need the @mp: parts. Also, since OpenXML needs a complete document I added a single root node.

declare @i int;

exec sp_xml_preparedocument @i output,
N'<doc><SHIPMENT xmlns="">
  <SHIPMENTNBR>US1011</SHIPMENTNBR>
  <SHIPMENTTYPE_CD>BK</SHIPMENTTYPE_CD>
  <LINESCAC>ACLH</LINESCAC>
  <TERMINAL_CD>USJAX</TERMINAL_CD>
  <ISSUE_DT>2006-08-17T14:23:09</ISSUE_DT>
  <VESSEL_CD>VES</VESSEL_CD>
  <VOYAGE_CD>VOYA</VOYAGE_CD>
  <POL />
  <POD>POD_0</POD>
  <SHIPMENTUNITS>
    <UNITNBR>1234</UNITNBR>
    <UNITTYPE_CD>CAR</UNITTYPE_CD>
    <MAKE>HON</MAKE>
    <MODEL>ACCO</MODEL>
    <COLOR>RED</COLOR>
    <FULLEMPTY_CD>F</FULLEMPTY_CD>
    <LENGTH>0011</LENGTH>
    <WIDTH>0022</WIDTH>
    <HEIGHT>0033</HEIGHT>
    <DIMENSIONUOM>C</DIMENSIONUOM>
    <WEIGHT>000010</WEIGHT>
    <WGTUOM>K</WGTUOM>
  </SHIPMENTUNITS>
</SHIPMENT>
<SHIPMENT xmlns="">
  <SHIPMENTNBR>IN2052</SHIPMENTNBR>
  <SHIPMENTTYPE_CD>BK</SHIPMENTTYPE_CD>
  <LINESCAC>ACLH</LINESCAC>
  <TERMINAL_CD>USJAX</TERMINAL_CD>
  <ISSUE_DT>2006-08-17T14:23:09</ISSUE_DT>
  <VESSEL_CD>VES</VESSEL_CD>
  <VOYAGE_CD>VOYA</VOYAGE_CD>
  <POL />
  <POD>POD_0</POD>
  <SHIPMENTUNITS>
    <UNITNBR>1234</UNITNBR>
    <UNITTYPE_CD>CAR</UNITTYPE_CD>
    <MAKE>HON</MAKE>
    <MODEL>ACCO</MODEL>
    <COLOR>RED</COLOR>
    <FULLEMPTY_CD>F</FULLEMPTY_CD>
    <LENGTH>0011</LENGTH>
    <WIDTH>0022</WIDTH>
    <HEIGHT>0033</HEIGHT>
    <DIMENSIONUOM>C</DIMENSIONUOM>
    <WEIGHT>000010</WEIGHT>
    <WGTUOM>K</WGTUOM>
  </SHIPMENTUNITS>
</SHIPMENT></doc>'

select *
from OpenXML(@i, '/doc/SHIPMENT')
        with (
                 id int '@mp:id'
               , nbr varchar(40) 'SHIPMENTNBR'
            -- , ...
               )

select *
from OpenXML(@i, '/doc/SHIPMENT/SHIPMENTUNITS')
        with (
                 id int '@mp:id'
               , ship_id int '@mp:parentid'
               , ship_nbr varchar(40) '../SHIPMENTNBR'
               , nbr varchar(40) 'UNITNBR'
            -- , ...
               )

exec sp_xml_removedocument @i

Best regards

Michael

Mike Dudeman on Sat, 28 Dec 2013 21:33:12


This sounds very familiar to the problems down in Utah with their closed communities.