Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 quick way to insert 1M record

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-05-28 : 11:57:58
I need to insert 1M for a test.

Is there a faster way to do it than this?

DECLARE @i int

set @i= 1

WHILE (@i <= 1000000)
BEGIN

insert into TempPin("ID", "Number") values(@i, 'mystyring')
SET @i = @i + 1
END

Kristen
Test

22859 Posts

Posted - 2010-05-28 : 11:59:59
Join/Cross join to Tally table

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-28 : 12:01:57
If you use version 2005 or later

One quick method is

Make ID as identity column and run


insert into TempPin(Number) values('mystyring')
GO 1000000

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-05-28 : 12:38:16
This is faster and has lower transactional overhead:
DECLARE @i int
SET @i=1

INSERT INTO TempPin([ID], [Number]) VALUES(@i, 'mystyring')

WHILE @i < 1000000 BEGIN
INSERT INTO TempPin([ID], [Number])
SELECT ID+@i, 'mystyring' FROM TempPin
WHERE ID <= 1000000-@i

SELECT @i=Max([ID]) FROM TempPin
END
I'd recommend a clustered index on the ID column if you don't already have one (clustered primary key is also fine). It should improve the performance.
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-05-28 : 16:01:35
thanks everyone -- went with the join option
Go to Top of Page
   

- Advertisement -