Sql Query to Insert Records from Batch wise Process


  • Hi,

    I have a table with 10 million of records and i want insert into batch wise in a existing diffent table. If i have used two method of Row by Row insert into records from existing table and use Cursor loop insert in data.But It will take to time to insert the i have decide batch wise to insert 1000 records once and again 1000 records second time. so how to write query pls help..because of performance wise i have use this method.

    here my script.

    set ansi_warnings off

    declare @row int
    declare @single int
    declare @time datetime
    declare @jurisID char(4)

    set @row = (select count(Rows) From Temp_SrcCodmap)
    set @single = 1--(select top 1 Rows  From Temp_SrcCodmap where Processed = 0)

    while @single <= @row
    set @time = (select getdate())
    print @time

    set @jurisID = (select JurisID from Temp_SrcCodmap where Rows = @single)

    insert HamiltonMidPolice.dbo.SrcIncAddr
          , AddrSeqNo
          , Location
          , Premise
          , Prefix
          , StreetName
          , StreetType
          , StreetSuffix
          , AptNo
          , intPrefix
          , intStreetName
          , intStreetType
          , JurisID

    , ROW_NUMBER() over (PARTITION by PnxCodeValue, IncidentIDRef order by ltrim(rtrim(GAINCD)) desc) AS ADDRSEQNO
    From Tem_SrcAddr where PnxCodeValue = @jurisID and Processed = 0

    update dbo.Imp_cad set Processed = 1 where Processed = @single

    set @single = @single + 1


    Select * From Temp_SrcCodmap

    Wednesday, May 29, 2013 7:26 AM


  • Try SSIS or DTS to do fast data processing and batch by batch

    Please mark as helpful and propose as answer if you find this as correct!!! Thanks, Rakesh.

    Wednesday, May 29, 2013 7:56 AM
  • Hi,

    If you are handling huge set of records, then i would suggest to use SISS package.

    If you are using sql 2008, then create a package using DATA FLOW TASK. You have options to specify Rows per batch and Maximum insert commit size. The below link might help you.

    If you are using sql 2000, then for DTS package.


    Best Regards, Venkat

    Wednesday, May 29, 2013 8:01 AM
  • Please take care of exceptions that may happen while transfering data, which i could not find in your script.

    SSIS or DTS can be an option.

    Regards, RSingh

    Wednesday, May 29, 2013 8:15 AM
  • Hi Venkat,

    Thanks for reply,

    I have Try to  SSIS Package also,  SQL Server Implementing batch processing in SSIS Package.

    It Would be take to Row by Row Process but I want insert 1000 records once and again 1000 records second time. There is not option in SSIS Package. So that is I try to SQL Query Batch Processing.

    Here for I try to Link.

    Thanks & Regards,



    Wednesday, May 29, 2013 8:35 AM

All replies