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

  • SEARCH
  • Home
  • 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 3635828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:57:13+00:00 2026-05-19T00:57:13+00:00

I am having a tough time struggling with XML within Sql Server 2008. I

  • 0

I am having a tough time struggling with XML within Sql Server 2008. I have an xml document that I need to parse into a rowset. I’m trying to come up with a sample query, that when executed against the blob, returns results such as:

ID                                   Status
--------                             -------- 
3765aaf7-afaa-4a46-8499-5a61b607692c 0
1234aaf7-afaa-4a46-8499-5a61b607692c 1

Here is the sample XML:

DECLARE @xml XML

SET @xml = '<Batch xmlns="urn:mynamespace">
  <request xmlns:b="urn:mymessages">
    <b:Batches xmlns:c="urn:myentities">
      <c:BatchEntry>
        <c:ID>3765aaf7-afaa-4a46-8499-5a61b607692c</c:ID>
        <c:Status>0</c:Status>
      </c:BatchEntry>
      <c:BatchEntry>
        <c:ID>1234aaf7-afaa-4a46-8499-5a61b607692c</c:ID>
        <c:Status>1</c:Status>
      </c:BatchEntry>
    </b:Batches>
    <b:BatchID>8492cbaa-eea5-479d-86c4-60cb62ac4b7c</b:BatchID>
  </request>
</Batch>'

The closest I’ve come is with:

SELECT T.a.query('.') AS ID 
FROM @xml.nodes('/*[local-name() = "Batch"]/*[local-name() = "request"]/*[local-name() = "Batches"]/*[local-name() = "BatchEntry"]/*[local-name() = "ID"]/text()') T(a)

But I cannot figure out how to basically select the ‘Batches’ node and then return a rowset for each ‘BatchEntry’. I feel like I just don’t understand some of the Sql Server xml operators just yet.. 🙂

Your help greatly appreciated!


UPDATE 2011-01-07

With @IanC answer below, I was able to get a complete working instance, with xml namespaces, of what I needed to do. I’m updating here just in case someone else runs across it.

DECLARE @xml XML

SET @xml = 
'<Batch xmlns="urn:mynamespace">
  <request xmlns:b="urn:mymessages">
    <b:Batches xmlns:c="urn:myentities">
      <c:BatchEntry>
        <c:ID>3765aaf7-afaa-4a46-8499-5a61b607692c</c:ID>
        <c:Status>0</c:Status>
      </c:BatchEntry>
      <c:BatchEntry>
        <c:ID>1234aaf7-afaa-4a46-8499-5a61b607692c</c:ID>
        <c:Status>1</c:Status>
      </c:BatchEntry>
    </b:Batches>
    <b:BatchID>8492cbaa-eea5-479d-86c4-60cb62ac4b7c</b:BatchID>
  </request>
</Batch>'

DECLARE @sp int
DECLARE @hxml int
DECLARE @Result int

DECLARE @t table 
(
    ID UNIQUEIDENTIFIER,
    BatchStatus INT   
)

EXEC    @sp = sp_xml_preparedocument @hxml OUTPUT, @xml
,
'<Batch 
    xmlns:a="urn:mynamespace"
    xmlns:b="urn:mymessages"
    xmlns:c="urn:myentities"
/>'

if @sp != 0 begin
    SET @Result = '0'
    RETURN
end

INSERT INTO @t
SELECT *
FROM OPENXML (@hxml, '/a:Batch/a:request/b:Batches/c:BatchEntry', 2)
WITH
(
    ID          UNIQUEIDENTIFIER    'c:ID',
    BatchStatus INT                 'c:Status'
)

SELECT * FROM @t

EXEC    sp_xml_removedocument @hxml;
  • 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-19T00:57:14+00:00Added an answer on May 19, 2026 at 12:57 am

    Try this as a general pattern. If you need more help with it, let me know and I’ll tailor it to your XML.

    DECLARE @t table (
        ProductTypeID   int,
        LowMin          real,
        HiMax           real,
        ParamTypeID     int,
        ParamWeight     real,
        Low             real,
        Hi              real,
        Mode            tinyint         
    )
    
    EXEC    @sp = sp_xml_preparedocument @hxml OUTPUT, @XMLText
    
    if @sp != 0 begin
        SET @Result = '0'
        RETURN
    end
    
    INSERT INTO @t
    SELECT      *
    FROM        OPENXML (@hxml, '/query/product/param/item', 2)
    WITH        (
                ProductTypeID   int     '../../@type',
                LowMin          real    '../@lowMin',
                HiMax           real    '../@hiMax',
                ParamTypeID     int     '../@type',
                ParamWeight     real    '@weight',
                Low             real    '@low',
                Hi              real    '@hi',
                Mode            tinyint '@mode'
                )
    
    EXEC    sp_xml_removedocument @hxml;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a tough time deploying a web site to IIS 7 on
I'm having a tough time trying to find clear and concise examples of how
I am still new at PHP/Javascript/Ajax and am having a very hard time (been
I have a BindingList<> of a class set to the DataSource property of a
(Sorry if this is a dupe) I've just spent a long time trying to
I'm writing a small audio application (in Silverlight, but that's not really relevant I
I have a spreadsheet with a dataset of a number of transactions, each of
I'm using CakePHP to produce a Facebook application (though the problem is not Facebook
I think over the last year and a half, while experimenting with Cocoa and
Mono is really awesome. Some of our applications worked in linux out of the

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.