Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3690748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:04:12+00:00 2026-05-19T04:04:12+00:00

I am trying to insert data using a XML in SQL Server. The XML

  • 0

I am trying to insert data using a XML in SQL Server. The XML I am using is

<ArrayOfInfringementEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <InfringementEntity>
        <infringementNumber>12345678911</infringementNumber>
        <issueAgency>017</issueAgency>
        <infringementType>1A</infringementType>
        <infringementStatus>0</infringementStatus>
        <batchRecordId>0</batchRecordId>
        <incidentDate xsi:nil="true" />
        <infringementSource>OTS</infringementSource>
        <TypeOfNotice>0</TypeOfNotice>
        <offenceEntity>
            <offenceCode>7777</offenceCode>
            <offenceDate>1999-05-31T00:00:00</offenceDate>
            <offenceTime>121212</offenceTime>
            <offenceLocation>ST56789</offenceLocation>
            <offenceOwnerType>0</offenceOwnerType>
            <offenceSuburb>SOUTH YARRA</offenceSuburb>
            <site>ST56789</site>
            <detectedSpeed>70</detectedSpeed>
            <allegedSpeed>60</allegedSpeed>
            <permittedSpeed>50</permittedSpeed>
            <timeInRedLight>40</timeInRedLight>
            <tollAmount>140</tollAmount>
            <enforcementAllowance>310</enforcementAllowance>
            <lookUpFee>510</lookUpFee>
            <invoiceFee>130</invoiceFee>
        </offenceEntity>
        <vehicleEntity>
            <vehicleClass>2</vehicleClass>
            <vehicleMake>BMW</vehicleMake>
            <vehicleModel>FOUR WHEELER</vehicleModel>
            <bodyType>HEAVY</bodyType>
            <primaryColour>GRN</primaryColour>
            <manufactureYear>2010</manufactureYear>
            <gvm>111</gvm>
            <gcm>210</gcm>
            <registrationNumber>CBD-1111</registrationNumber>
            <registrationState>VIC</registrationState>
        </vehicleEntity>
        <obligationNumber>obligation1</obligationNumber>
        <isDebtorDeceased>false</isDebtorDeceased>
    </InfringementEntity>
</ArrayOfInfringementEntity>

I want to shred this XML in a temp table. I tried using

create table #InfTemp

(infringementNumber Varchar(10),issueAgency varchar(5),infringementType varchar(5), offenceCode int,vehicleClass int,obligationNumber varchar(11)
)

Insert into #InfTemp
   SELECT  PLIxml.infringementNumber, PLIxml.issueAgency,PLIxml.infringementType,  
    PLIxml.offenceCode , PLIxml.vehicleClass ,PLIxml.obligationNumber 
  FROM  OPENXML (@output, 'ArrayOfInfringementEntity/InfringementEntity',2)   
  WITH
  (  infringementNumber Varchar(10),issueAgency varchar(5),infringementType varchar(5),offenceCode int,vehicleClass int,obligationNumber varchar(11)
   ) PLIxml 

But I am getting Null in Offencecode and vehicle class. And as I understand, this is justified as <offenceCode> is a child node of <offenceEntity>. And I am not exclusively reading <offenceEntity> node. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:04:13+00:00Added an answer on May 19, 2026 at 4:04 am

    Based on your XML, you could use this XQuery SELECT to extract your items from the XML:

    select
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
        @input.value('(/ArrayOfInfringementEntity/InfringementEntity/obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'
    

    Replace @input with the variable or column that holds your XML (I’ve used @input as a test bed in my tests).

    The output looks like this:

    InfringementNumber  Issue Agency    Infringement Type   Offence Code    Vehicle Class   Obligation Number
    1234567891              017                1A              7777            2                obligation1
    

    And of course, you can also do an INSERT INTO .... and use the output from this SELECT as the values to insert.

    Update: if your XML column contains multiple entries (of /InfringementEntity inside the /ArrayOfInfringementEntity), you need to use a SELECT like this:

    SELECT
        InfrEntity.value('(infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
        InfrEntity.value('(issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
        InfrEntity.value('(infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
        InfrEntity.value('(offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
        InfrEntity.value('(vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
        InfrEntity.value('(obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'
    from
        (yourXMLcolumn).nodes('/ArrayOfInfringementEntity/InfringementEntity') as ArrInfr(InfrEntity)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to insert new data into an existing XML file, but it's
I received a MySQL data dump and am trying to insert the data into
I'm trying to make an ajax call to grab session data to insert into
I am trying to INSERT INTO a table using the input from another table.
I'm trying to insert a column into an existing DataSet using C#. As an
I'm trying to insert some import lines into a python source file, but i
I am trying to insert about 50,000 objects (and therefore 50,000 keys) into a
I am trying to insert a time only value, but get the following error
I'm trying to insert a comment character into a string something similar to this:
I'm trying to insert an li element into a specific index on a ul

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.