Program to display text 'N'th time using a recursive CTE in SQL Server

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

;With Cte AS
(
SELECT
1 AS rn,
'Hello World' AS [Text]

UNION ALL

SELECT
rn+1,
'Hello World' AS [Text]
FROM Cte
WHERE rn<@N
)

SELECT
[Text]
FROM Cte
OPTION (MAXRECURSION 0)


In the above program we are using a recursive CTE that will execute till the condition is true and finally prints 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