Solved

Can someone algorithmically explain this TSQL UDF with XML?

Posted on 2012-04-11
1
452 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
ID: 37834841
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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