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

Comments

  1. Hi,
    I read your whole blog. But still I am in confusion of understanding this blog on windows SQL Tables. Can you explain more in detail.

    ReplyDelete

Post a Comment

I Post più popolari

SQL Server, execution plan and the lazy spool (clearly explained)

SQL Server, datetime vs. datetime2

How to solve EXECUTE Permission denied on object 'sp_send_dbmail'