Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert n number of records in a table in a single command

I want to insert 10000 records in a table and i am currently writing this code in sql server 2005

declare @n decimal(10,0);
set @n = 0;
while ( @n < 10000)
begin
   insert into table1 values (@n+1)
   set @n = @n + 1
end

in the above code insert command performs 10000 times is there any single command exists to do so.

like image 418
Haider Ali Wajihi Avatar asked Jan 18 '23 08:01

Haider Ali Wajihi


1 Answers

Also you can use sys objects to your advantage:

INSERT INTO table1(n)
SELECT TOP 10000 ROW_NUMBER() OVER(ORDER BY a.object_id) AS n FROM sys.objects a CROSS JOIN sys.objects b
GO
like image 123
cairnz Avatar answered Apr 27 '23 07:04

cairnz