Search Suggest

SQL Server, generate a number of rows

Hi Guys,

Today, after the last tips SQL Server, Concatenates text from multiple rows into a single string!

I want to show you another tips!

I suggest you bookmark this page. This way, when you need it, a copy and paste will suffice!

How many times did it take you to have a table with a fixed number of rows at your fingertips?

Here it is!

 

Generate a number of rows

Suppose you want to generate 10 rows.

Just write (or copy and paste) this command:


;WITH progr(x) AS
(
SELECT 1
UNION ALL
SELECT x+1 FROM progr WHERE x < 10
)
SELECT x FROM progr
OPTION (MAXRECURSION 0);

And here is the result:



What we have done? simple: we used a CTE in order to trigger recursion.

That's all for today! Stay tuned mate!
Luca

Luca Biondi @ SQLServerPerformance blog!







Next post:

Previous post: SQL Server 2019 and the Approx_Count_Distinct function

Post a Comment