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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:39:11+00:00 2026-05-16T21:39:11+00:00

I have an xml column and I want to persist a node count in

  • 0

I have an xml column and I want to persist a node count in it in an adjacent column.

For example, in an xml given below, I have 3 nodes a and so I want to output it.

The problem that I have, however, is that I can’t manage to create a generic schema-bound function that would take @xml and @nodeName so as to return the count of a specified node.

create function dbo.fnXmlCount_a (@xml xml) 
returns int with schemabinding
as begin
return (@xml.value('count(//a)', 'int'))
end

declare @xml xml;
set @xml = '
<r>
 <a></a>
 <b></b>
 <a></a>
 <c>
  <a></a> 
 </c>
</r>'

select dbo.fnXmlCount_a(@xml) aCount

Result of the last operation is:

aCount
3

And then I create a column on that table that has this xml field like so:

alter table [XmlTable] add column [aCount] as (dbo.fnXmlCount_a([xml])) persisted

But what I really would like to do is this:

alter table [XmlTable] add column [aCount] as (dbo.fnXmlNodeCount([xml], 'a')) persisted

Thanks!

Update:

We all know that // is really slow, so it would be great if you could figure out how to pass this path to search in in another variable; by default, this searching path would be //.

  • 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-16T21:39:12+00:00Added an answer on May 16, 2026 at 9:39 pm

    Here you go:

    create function dbo.fnXmlCount (@xml xml, @name nvarchar(max))  
    returns int with schemabinding 
    as begin 
    declare @count int
    select @count = count(*)
    from @xml.nodes('//*') as t(c)
    where t.c.value('local-name(.)', 'nvarchar(max)') = @name
    return @count
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML valued column that has multiple parent nodes. I need to
I have a questionnaire stored in a XML column that I want to store
I have mysql table that has a column that stores xml as a string.
I have an XML column in one of my table. For example I have
I have a table that contains a column of XML Datatype (column name FileContent).
I have some XML that stores column information and row data from a table
I have xml column in a table and I want to parse the xml
I have an XML column in a table; I want to promote a certain
I have an XML column in SQL Server that is the equivalent of: <Test
I'm hand-coding Linq to SQL Entities and have a large XML column that I

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.