Insert certain limited numbers in a SQL Server column

I want to write an SQL query that inserts incremental numbers starting with a specific number (e.g. 100) and ends with another specific number (e.g. 3000) in a table column in SQL Server, but I don't know how to do this.

For instance:
I want to insert from 100 to 3000 in categoryID (column) from Category (table)

Many thanks

+6
source share
2 answers
 DECLARE @id INT SET @id = 100 WHILE (@id <= 300) BEGIN insert into categories (categoryID) values (@id) SELECT @id = @id + 1 END 
+7
source

The same, but using recursive CTE:

 DECLARE @i INT DECLARE @n INT SET @i = 100 SET @n = 3000 ;WITH t(c) AS ( SELECT @i UNION ALL SELECT c + 1 FROM t WHERE c < @n ) INSERT INTO Categories(categoryID) SELECT c FROM t OPTION(MAXRECURSION 3000) 
+1
source

Source: https://habr.com/ru/post/915549/


All Articles