• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 603
  • Last Modified:

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
0
Varshini
Asked:
Varshini
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
kamindaCommented:
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

0
 
Alpesh PatelAssistant ConsultantCommented:
Declare @Tmp numeric
Set @tmp = 200001    

While @tmp <= 900004    
begin
inset into Table values (1, @tmp, '21-mar-2011')
set  @tmp =  @tmp+ 1
end
0
 
Asim NazirCommented:
User Bulk insert command. Details: http://msdn.microsoft.com/en-us/library/ms188365.aspx
0
 
GugroCommented:
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now