Solved

Can someone algorithmically explain this TSQL UDF with XML?

Posted on 2012-04-11
1
443 Views
Last Modified: 2012-08-14
Can someone please provide an algorithmic description of what this TSQL UDF is doing? I am not familiar with XML for TSQL.

FUNCTION [Common].[XmlArrayOfInt]

(@Xml AS XML (Common.ArrayOfIntegersSchema))

RETURNS @Result TABLE (Value INT NOT NULL)

AS

BEGIN
      INSERT INTO @Result(Value)
      SELECT T.c.value('.','int') AS Value
      FROM @Xml.nodes('/ArrayOfInt/int') AS T(c)
      RETURN
END

Also are value and nodes built-in TSQL functions? And what is '/ArrayOfInt/int'?

Thanks!
0
Comment
Question by:pae2
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
To start at the end: '/ArrayOfInt/int' is an xpath query with which you can select parts of an xml document. This one just selects all 'int' nodes under the root node 'ArrayOfInt' but it can contain a lot more complicated queries in itself.

Given an xml document as input, this function returns a table that contains all values in the 'int' nodes.

So given this xml document:

<ArrayOfInt>
  <int>6</int>
  <int>12</int>
  <int>7</int>
</ArrayOfInt>

Open in new window


It would return:

Value
-----
    6
   12
    7

Open in new window


Yes, @Xml.nodes and T.c.value are built-in functions to work with xml data. See for more info under the heading 'Methods on XML Data Type' at this page: http://msdn.microsoft.com/en-us/library/ms345117(v=sql.90).aspx#sql2k5xml_topic3
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now