We help IT Professionals succeed at work.

How to insert thousands of records in sql table ?

Varshini
Varshini asked
on
Medium Priority
676 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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

Commented:
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

Solution Architect at TCS (SAFe®Agilist, TOGAF® Foundation, PSM I®, MCP, MCSD)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Asim NazirProject Manager

Commented:
User Bulk insert command. Details: http://msdn.microsoft.com/en-us/library/ms188365.aspx

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.