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 8078743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:53:40+00:00 2026-06-05T15:53:40+00:00

I am using SQL Server 2008 R2 and my stored procedure takes in a

  • 0

I am using SQL Server 2008 R2 and my stored procedure takes in a structured XML which has multiple levels like this:

DECLARE @XML xml = 
'<Main>
    <User id="1">
        <Departments isSingle="0">
            <Department id="1">Admin</Department>
            <Department id="2">HR</Department>
            <Department id="3">Development</Department>
        </Departments>
    </User>
    <User id="2">
        <Departments isSingle="1">
            <Department id="1">Admin</Department>            
        </Departments>
    </User>
</Main>'

From the above example I want to get 2 columns for Users with multiple departments (isSingle=”0″) where first column is the user id and second column is the whole <Departments> XML.

I can get the user id with the following query but how to get the Departments section as an XML:

SELECT
      T.C.value('(../@id)','int') AS UserID
    , T.C.value('(../Departments)[1]','nvarchar(max)') AS DepartmentsXML 
FROM 
@XML.nodes('/Main/User/Departments[@isSingle="0"]') AS T(C)

It does not allow me to use xml as datatype in place of nvarchar(max)

If details are not clear let me know and I will try to refine it. Any help is appreciated.

  • 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-06-05T15:53:42+00:00Added an answer on June 5, 2026 at 3:53 pm

    You’re close – just use .query('.') for your second column:

    SELECT
          T.C.value('(../@id)','int') AS UserID, 
          T.C.query('.') AS DepartmentsXML 
    FROM 
    @XML.nodes('/Main/User/Departments[@isSingle="0"]') AS T(C)
    

    This will return the XML fragment as selected by the .nodes() call, as an XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 Enterprise. I am wondering whether this stored procedure
Using sql server 2008, I have a simple stored procedure, the contents of which
I am using Sql Server 2008. My Stored Procedure accepts almost 150 parameters. Is
I'm using SQL Server 2008. I have an interesting scenario where a stored procedure
I have a problem when debugging stored procedure using SQL server Management Studio 2008.
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
Using SQL Server 2008, I have two stored procedures - create procedure [dbo].[SH_Export_data] (@unit
If, using the following code in a MS SQL Server 2008 stored procedure: DECLARE

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.