Link to home
Start Free TrialLog in
Avatar of Varshini
Varshini

asked on

How to insert thousands of records in sql table ?

i have the following table with following columns.
how to insert transid 200001  to 900004  
  in a single inert command, other columns values are same.


Companyid    TransId     Date  
1                     200001    21-Mar-2011
1                     200002    21-Mar-2011
1                    200003    21-Mar-2011
1                    200004    21-Mar-2011
upto
1                     900004    21-Mar-2011
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

please try this:
;with cte as (
  select top 10 row_number() over (order by name) -1 rn from sys.objects 
)
, x as (
  select u1.rn + 10* u2.rn + 100*u3.rn + 1000* u4.rn + 10000*u5.rn + 100000*u6.rn  transid
    from cte u1
       , cte u2
       , cte u3
       , cte u4
       , cte u5
       , cte u6
  where u6.rn between 2 and 9
)
insert into yourtable
select 1 companyid, x.transid, convert(datetime, '2011-03-21', 120) [date]
  from x
 where transid >= 200001 
   and transid <= 900004
 order by transid

Open in new window

You can use a while loop
DECLARE @i INT
DECLARE @rows_to_insert INT 
SET @i = 200001    
SET @rows_to_insert = 900005   

WHILE @i < @rows_to_insert    
BEGIN     
INSERT INTO TABLE (Companyid,TransId,Date)
SELECT 1,@i,'2011-03-21'
SET @i = @i + 1 
END

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alpesh Patel
Alpesh Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gugro
Gugro

Why would you do that ?
> how to insert transid 200001  to 900004  
> in a single inert command, other columns values are same.
I would write a UDF ( User defined function ) which returns me for every TransID between 200001  to 900004  the company ID 1 and the date 21-Mar-2011