Skip navigation
Currently Being Moderated

Inserting large amount of records in loop

Oct 5, 2011 6:10 PM

I have a loop that goes from current date to the end of the year and inserts a record for each day up to the end of the year

 

The question is, is there a better way to do this without having to loop..insert...loop..insert so many times!

 

Here's the code -->

 

<!--- OFFSET FOR TIME DIFFERENCE ON SERVER TO LOCAL TIME --->

<CFSET timedifference="2">

 

<CFSET TestAffUID = "1234">

 

<CFSET StartDate = #Now()#>

<CFSET EndDate = "01/01/2012">

<CFSET NumberOfDays = #datediff('d',StartDate,EndDate)#>

 

 

<CFLOOP INDEX="DateCounter" FROM="0" TO="#NumberOfDays#">

 

<!--- CREATE THE DATE --->

 

<CFSET DateToAdd = DateAdd( "d", Datecounter, StartDate )>

 

<!--- INSERT THE DATE INTO DBASE --->

 

<CFQUERY NAME="InsertDate" DATASOURCE="#datasource#">

INSERT INTO impressions

(

imp_aff_uid,

imp_date,

imp_counter

)

VALUES

(

#TestAffUID#,

#createodbcdate(dateadd('h',timedifference,DateToAdd))#,

0

)

</CFQUERY>

 

</CFLOOP>

 
Replies
  • Currently Being Moderated
    Oct 6, 2011 2:52 AM   in reply to ACS LLC

    You can insert all records in a single query, something like this:

     

    insert into table (field1, field2, ...)

    select 'field1value1', 'field2value', ...

    union all

    select 'field1value2', 'field2value2', ...

    union all

    ...

     

    --

    Mack

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 6, 2011 5:39 AM   in reply to mack_

    If you go with mack_'s method, you will have to do something to escape the extra "union" keyword generated in your loop.

     

    If you have records from last year, you can use and sql dateadd function to do everything in one query.  If you go with this approach, you will have to figure out what to do about leap years.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 6, 2011 9:37 AM   in reply to ACS LLC

    You currently have your query inside a loop.  You can also put loops inside queries to achieve the syntax mack_ suggested.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 6, 2011 12:06 PM   in reply to ACS LLC

    You can have SQL Server do the work, less database connections:

     

    <CFQUERY NAME="InsertDate" DATASOURCE="#datasource#">

     

    DECLARE @Start int,

            @End int,

            @NextYear varchar(20),

            @ImpDate smalldatetime

     

    SET @Start = 0

    SET @NextYear = CONVERT(varchar(4), year(DATEADD(yyyy, 1, GETDATE() ))) + '-01-01 00:00:00'

    SET @End = (SELECT DATEDIFF (d , GETDATE(),  @NextYear ))

     

    WHILE (@Start < @End)

     

    BEGIN

     

        SET @ImpDate = (    SELECT DateAdd( hh, 2, DateAdd( d, @Start, getDate() ))    ) /*this is what you intended with the #createodbcdate(dateadd('h',timedifference,DateToAdd))# except that createodbcdate() does not capture the time, you really need createodbcdatetime() */

        /* SET @ImpDate = (    SELECT DateAdd( d, @Start, CONVERT(date, getDate(), 101) )    ) --if you want the time to be 00:00:00 */

        /* Print(@ImpDate) */

     

        INSERT INTO impressions ([imp_aff_uid], [imp_date], [imp_counter])

        VALUES (1234, @ImpDate, 0)

     

        SET @Start = @Start +1

     

    END

    </CFQUERY>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points