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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:07:51+00:00 2026-05-24T14:07:51+00:00

I have an XML hierarchy like this in an XML type column of a

  • 0

I have an XML hierarchy like this in an XML type column of a table with 10,000 records-

<Root>
     <Elem1>
         <Parent1>
              <Separator>
                  <Child1/>
              </Separator>
         </Parent1>
     </Elem1>
</Root>

I have a query like this –

DECLARE @Root VARCHAR(50)
DECLARE @Entity VARCHAR(50)
DECLARE @ParentNode VARCHAR(50)
DECLARE @Separator VARCHAR(50)
DECLARE @ChildNode VARCHAR(50)


SET @Root = 'Root'
SET @Entity = 'Elem1'
SET @ParentNode = 'Parent1'
SET @Separator = 'separator'
SET @ChildNode = 'Child1'

select Parent.P.value('.', 'varchar(max)') as MyValue, 
T.uniqueId, T.XMLCol
from [XMLTable] as T
cross apply 
 (SELECT
         XMLTable.XMLCol.query('(/*[local-name()=sql:variable("@Root")]/*[local-name(.)=sql:variable("@Entity")]/*[local-name(.)=sql:variable("@ParentNode")]/*[local-name(.)=sql:variable("@Separator")]/*[local-name(.)=sql:variable("@ChildNode")])[1]'
 )  as Parent(P)

How can I further Optimize this query. Currently, it is taking 2 secs and if I do further INNER JOINS, it adds up to the time. I tried creating a PRIMARY Index on the XML Column, but it takes more time !

EDIT- If I hardcode the path instead of using variables, then it takes less than a second. But, I want it in a Table-Valued function and cannot hardcode path ?


EDIT – Solution

select x.value('(Parent1/Separator1/Child1)[1]', 'varchar(max)') as Col1,
x.value('(Parent2/Separator2/Child2)[1]', 'varchar(max)') as Col2,
x.value('(Parent3)[1]', 'varchar(max)') as Col3
from [XMLTable] T 
cross apply T.XMLCOL.nodes('/Root/Elem1') a(x)

The above query takes just about one sec. It seems to be the fastest of all. So, instead of going for a Table valued function with params, the above query can be dynamically prepared and executed on the fly using ADO.NET capabilities

Correct me please … ?

  • 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-24T14:07:52+00:00Added an answer on May 24, 2026 at 2:07 pm

    If you only want one value out of every row there is no need to use cross apply.

    select XMLCol.value('(/*[local-name()=sql:variable("@Root")]
                          /*[local-name(.)=sql:variable("@Entity")]
                          /*[local-name(.)=sql:variable("@ParentNode")]
                          /*[local-name(.)=sql:variable("@Separator")]
                          /*[local-name(.)=sql:variable("@ChildNode")])[1]', 'varchar(max)')
    from XMLTable
    

    Another way to get the same is to use FLWOR. In my limited tests this will run a bit faster.

    select XMLCol.value('(for $n1 in /*,
                              $n2 in $n1/*,
                              $n3 in $n2/*,
                              $n4 in $n3/*,
                              $n5 in $n4/*
                          where $n1[local-name(.) = sql:variable("@Root")] and
                                $n2[local-name(.) = sql:variable("@Entity")] and
                                $n3[local-name(.) = sql:variable("@ParentNode")] and
                                $n4[local-name(.) = sql:variable("@Separator")] and
                                $n5[local-name(.) = sql:variable("@ChildNode")]
                          return $n5
                         )[1]', 'varchar(max)')
    from XMLTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have an XML document that models this hierarchy of tasks: 1 Customer 1.1
I have a xml file that looks like this: <DataTalk> <Posts> <TalkPost> <PostType>dialog</PostType> <User>ABBE</User>
I have a collapsible menu item that is defined in XML like this: <item
We have tree structure like this: <Tree> <child1> <child2> <child2> </child1> </Tree> Here the
I'm creating an XML hierarchy that looks something like this: element A 0+ element
I have a hierarchy of classes like this: MyBox | |->ImageBox |->GalleryBox |->MovieBox |->
I have a hierarchy of classes that are serialised to XML using XMLSerialiser .

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.