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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:07:08+00:00 2026-05-25T12:07:08+00:00

Is it possible to achieve the following table from of output using XQuery in

  • 0

Is it possible to achieve the following table from of output using XQuery in SQL Server

Edit: A change in my requirement, Consider i have a table which stores the below xml and also UserID

DECLARE  @XML xml
set @XML = '<Security>
             <FiscalYear Name="2012">
            <Country Id="204">
              <State Id="1">
                <City Id="10"></City>
              </State>
              <State Id="2">
                <City Id="20"></City>
                <City Id="30"></City>
              </State>
              <State Id ="3"></State>
              </Country >
            </FiscalYear>
        </Security>'

CREATE TABLE #tmp_user
(UserID INT,SecurityXML XML)

INSERT INTO #tmp_user
        ( UserID, SecurityXML )
VALUES  ( 1, 
          @XML
          )

Now how can i get a o/p like

Output:

 UserID StateID       CityID
     1      1           10
     1      2           20
     1      2           30
     1      3            0

Is it possible to achieve?

  • 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-25T12:07:09+00:00Added an answer on May 25, 2026 at 12:07 pm

    I modified your XML a bit because it was invalid. Change the end tag </Subsidiary> to </Country>.

    declare @XML xml
    set @XML = 
    '<Security>
       <FiscalYear Name="2012">
         <Country Id="204">
          <State Id="1">
            <City Id="10"></City>
          </State>
          <State Id="2">
            <City Id="20"></City>
            <City Id="30"></City>
          </State>
          <State Id ="3"></State>
        </Country>
       </FiscalYear>
     </Security>'
    
    select S.N.value('@Id', 'int') as StateID,
           coalesce(C.N.value('@Id', 'int'), 0) as CityID
    from @XML.nodes('/Security/FiscalYear/Country/State') as S(N)
      outer apply S.N.nodes('City') as C(N)
    

    A version using a table instead of XML variable

    select T.UserID,
           S.N.value('@Id', 'int') as StateID,
           coalesce(C.N.value('@Id', 'int'), 0) as CityID
    from #tmp_user as T
      cross apply T.SecurityXML.nodes('/Security/FiscalYear/Country/State') as S(N)
      outer apply S.N.nodes('City') as C(N)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: delete duplicate records in SQL Server I have a table in which
I have a table with the following fields in an SQL Server 2005 database:
we have following table (output is already ordered and separated for understanding): | PK
It it possible to achieve the following layout in CSS using a <ul></ul> and
I would like to form an SQL query to achieve the following: I have
Is it possible to achieve this task with a single MySQL query? Table urls
I am wondering if it's possible to achieve this using the jQuery UI Slider.
Is there any possible way to achieve this? For instance, I have an I/O
I have the following code, bar the possible typo should select 3 columns, the
I have a table with the following structure: (table_name, column_name) and for each row

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.