Program to display text 'N'th time using XQuery in SQL Server

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 294
DECLARE @N INT  = 10

DECLARE @x XML
SELECT @x = REPLICATE ('<x>Hello World</x>', @N)
SELECT
i.value('.', 'VARCHAR(MAX)') [Text]
FROM @x.nodes('//x') x(i)


In the above program first we create N nvarchar taxts using the REPLICATE function.That we are storing in an XML variable which on the other hands becomes the nodes for XML.

<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>
<x>Hello World</x>


Once done then by using the XQuery, we are picking up the values of the node and displaying the result

/* Result

Text
------
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
*/

Comments or Responses

Login to post response