OpenXML

  •  

    This is a sample of my XML

    <ShipmentHeader SettleFlag="A">

    <Customer CustomerID="A" CustomerName="A"/>

    <SubCustomer SubCustomerID="A" SubCustomerName="A"/>

    </

    ShipmentHeader>

    and I want the following output as in single openXML command.

    SettleFlag CustomerId CustomerName SubCustomerId SubCustomerName

    Is this possible and if yes then how.

    The following query do not give me the value for CustomerId

    SELECT

    *

    FROM

    OPENXML( @docHandle,'/ShipmentHeader')

    WITH

    (

    [SettleFlag]

    varchar(100)

    ,CustomerID varchar(100) '/Customer/@CustomerID'

    ) X


    Kindest Regards,

    Amit Lohia

  • Strange XML ... But you can  do it like this :

     

    SELECT *

    FROM   OPENXML (@docHandle, '/ShipmentHeader',2)

             WITH (SettleFlag      char(1)      '@SettleFlag',

                   CustomerID      varchar(10)  'Customer/@CustomerID',

                   CustomerName    varchar(10)  'Customer/@CustomerName',

                   SubCustomerID   varchar(10)  'SubCustomer/@SubCustomerID',

                   SubCustomerName varchar(10)  'SubCustomer/@SubCustomerName' )

  • Hi Bert, I have a simlar question,except in my case, i have more than one subcustomer.

    ShipmentHeader SettleFlag="A">

    Customer CustomerID="A" CustomerName="A"/>

    SubCustomer SubCustomerID="A" SubCustomerName="A"/>

    SubCustomer SubCustomerID="B" SubCustomerName="B"/>

    /ShipmentHeader>

    Note: (Modified the above XML by deleting preceding "<" symbol)

    If i set the node to the top level as you suggested, with your code will i be able to get both subcustomers.

    Thanks in Advance

    Venkat

  • [font="Courier New"]I suppose there is only 1 customer, and 1 or more subcustoerms. This query would do the job :

    DECLARE @docHandle int

    DECLARE @doc varchar(1000)

    SET @doc ='

    <ShipmentHeader SettleFlag="A">

    <Customer CustomerID="A1" CustomerName="A"/>

    <SubCustomer SubCustomerID="AA" SubCustomerName="An"/>

    <SubCustomer SubCustomerID="BB" SubCustomerName="Bn"/>

    <SubCustomer SubCustomerID="CC" SubCustomerName="Cn"/>

    </ShipmentHeader>'

    EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc

    select C.*,S.*

    from

    (

    SELECT *

    FROM OPENXML(@docHandle, '/ShipmentHeader/Customer',2)

    WITH(SettleFlag char(1) '../@SettleFlag',

    CustomerID varchar(10) './@CustomerID',

    CustomerName varchar(10) './@CustomerName' )

    ) C

    full outer join

    (

    SELECT *

    FROM OPENXML(@docHandle, '//SubCustomer',2)

    WITH(SubCustomerID varchar(10) '@SubCustomerID',

    SubCustomerName varchar(10) '@SubCustomerName' )

    ) S on ( 1=1)

    But if you could change the xml like this, the query becomes much simplier :

    DECLARE @docHandle int

    DECLARE @doc varchar(1000)

    SET @doc ='

    <ShipmentHeader SettleFlag="A">

    <Customer CustomerID="A1" CustomerName="A">

    <SubCustomer SubCustomerID="AA" SubCustomerName="An"/>

    <SubCustomer SubCustomerID="BB" SubCustomerName="Bn"/>

    <SubCustomer SubCustomerID="CC" SubCustomerName="Cn"/>

    </Customer>

    </ShipmentHeader>'

    EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc

    SELECT *

    FROM OPENXML(@docHandle, '/ShipmentHeader/Customer/SubCustomer',2)

    WITH(SettleFlag char(1) '../../@SettleFlag',

    CustomerID varchar(10) '../@CustomerID',

    CustomerName varchar(10) '../@CustomerName',

    SubCustomerID varchar(10) '@SubCustomerID',

    SubCustomerName varchar(10) '@SubCustomerName' )[/font]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply