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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:08+00:00 2026-05-23T09:46:08+00:00

I have this variable: declare @xmlDoc XML it has the following xml stored in

  • 0

I have this variable:

declare @xmlDoc XML

it has the following xml stored in it:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <Table1>
    <Sharedparam>shared</Sharedparam>
    <Antoher>sahre</Antoher>
    <RandomParam2>Good stuff</RandomParam2>
    <MoreParam>and more</MoreParam>
    <ResultsParam>2</ResultsParam>
  </Table1>
  <Table1>       
    <RandomParam2>do you</RandomParam2>
    <MoreParam>think</MoreParam>
    <ResultsParam>2</ResultsParam>
  </Table1>
  <Table1>
    <Sharedparam>Last</Sharedparam>
    <Antoher> Set </Antoher>
    <RandomParam2> of </RandomParam2>
    <MoreParam>values</MoreParam>
    <ResultsParam>are here</ResultsParam>
  </Table1>
  <Table1 />
</NewDataSet>

I have this query that I am using to get the data:

declare @xmlDoc XML
set @xmlDoc = '' -- Stack Overflow could not handle the xml all on one line.

SELECT   -- Param 1
         TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
         TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,             
         -- Param2
         TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
         TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value, 

         -- Param3           
         TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
         TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,

         -- Param 4
         TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
         TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,

         -- Param 5
         TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
         TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value

 FROM    @xmldoc.nodes('/NewDataSet/Table1') AS TBL(SParam)  

I need a way to add to my results the order that they came from the xml file. (Which was the first instance of Table1, then the second….).

For reasons of SQL table variable limitations, I can’t use an identity column to keep this straight. (For other reasons, I don’t want to use a temporary table.)

I am hoping that there is a cool SQL XML function that will return some kind of internally assigned Node ID. (Or some other similar manner of ordering.)

Note, I do not control this XML Structure (I am a reader only) so I cannot make changes to add in an ID attribute.

Any advice would be great!

EDIT/Update:

I would really like to have this data like this:

1  |    SharedParam   |   shared
1  |    Antoher       |   sahre
1  |    RandomParam2  |   Good stuff
1  |    MoreParam     |   and more
1  |    ResultsParam  |   and more
2  |    RandomParam2  |   do you
2  |    MoreParam     |   think
2  |    ResultsParam  |   2
3  |    Sharedparam   |   Last
3  |    Antoher       |   Set 
.
.
.

But I am coming up short. I can get it into columns (more or less), but I don’t know how to do the numbering. If you have any ideas I would love to hear them.

EDIT:
I figured out the query to do this (with some help from the internet). It looks like this:

SELECT  TBL.SParam.value('local-name(.)[1]', 'varchar(50)') as ParamName,
        TBL.SParam.value('(.)[1]', 'varchar(50)') ParamValue, 
        TBL.SParam.value('for $s in . return count(../*[. << $s]) + 1', 'int') ParamPosition,
        TBL.SParam.value('for $s in . return count(../../*[. << $s]) - 1', 'int') ParamIteration 
FROM    @xmldoc.nodes('/NewDataSet/Table1/*') AS TBL(SParam)
  • 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-23T09:46:08+00:00Added an answer on May 23, 2026 at 9:46 am

    You can use a number table and position()

     SELECT  N.Number as ID,
             -- Param 1
             TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
             TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,             
             -- Param2
             TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
             TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value, 
    
             -- Param3           
             TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
             TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
    
             -- Param 4
             TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
             TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
    
             -- Param 5
             TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
             TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
    
     FROM master..spt_values as N
        cross apply @xmldoc.nodes('/NewDataSet/Table1[position()=sql:column("N.Number")]') AS TBL(SParam)
     where N.type = 'P' and
           N.number between 1 and @xmlDoc.value('count(/NewDataSet/Table1)', 'int')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this XML variable: declare @xml xml='< Employee EmployeeId=1 FirstName=John LastName=Smith />
If I have a SQL script stored in a variable like this: DECLARE @SQL
I have this string stored in a variable: IN=bla@some.com;john@home.com Now I would like to
I have some text that is stored in a variable like this: <div class=foo>text
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
I have this table variable declaration followed by a query: DECLARE @CurrentItems TABLE (
i declare a variable name as Result1 this variable i have used in when
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have this array, for example (the size is variable): x = [1.111, 1.122,
Suppose that I have an integer variable in a class, and this variable may

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.