By combining multiple inserts into one bulk insert statement, you can reduce the total number of statements sent to the database, speeding up combined execution of your script.

The below example illustrates this concept using a ColdFusion code snippet.

<cfquery datasource="#application.strConfig.dsn#">
    INSERT INTO mytable (field1, field2)
    VALUES
    <cfloop list="#mylist#" index="listitem">
        (
            <cfqueryparam value="#field1value#">,
            <cfqueryparam value="#listitem#">
        )
        <cfif (listitem neq ListLast(mylist))>,</cfif>
    </cfloop>
</cfquery>

With each loop iteration, we check that the current list item is not the last item in the list. If it is not the last item, we place a comma after the insert, to specify that another insert is to follow.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *