How to insert thousands of records in sql table ?

Posted on 2011-04-27
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
1                     900004    21-Mar-2011
Question by:Varshini
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    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

    LVL 9

    Expert Comment

    You can use a while loop
    DECLARE @rows_to_insert INT 
    SET @i = 200001    
    SET @rows_to_insert = 900005   
    WHILE @i < @rows_to_insert    
    INSERT INTO TABLE (Companyid,TransId,Date)
    SELECT 1,@i,'2011-03-21'
    SET @i = @i + 1 

    Open in new window

    LVL 21

    Accepted Solution

    Declare @Tmp numeric
    Set @tmp = 200001    

    While @tmp <= 900004    
    inset into Table values (1, @tmp, '21-mar-2011')
    set  @tmp =  @tmp+ 1
    LVL 10

    Expert Comment

    by:Asim Nazir
    User Bulk insert command. Details:
    LVL 6

    Expert Comment

    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now