Skip to main content
Inspiring
May 27, 2009
Question

How to tell between update and insert ?

  • May 27, 2009
  • 6 replies
  • 1915 views

I have a form where a user can enter a value, press a button and anoher input would appear, add another value, press the button again, etc. When done, he submits and I use cfloop list to insert the data into a table.

When I bring up this form again, I check to see if there are any values in the table. If they are there, I display the values in a table, one per row.  At the end, I have the button again, just in case they need to add more values.

Here is the problem that I am running into and do not know how to solve. How do I know whether they added a new value, or simply changed one of the existing values ? Or what is they did both, change an exisitng value and add a new value ?

This is the code I use if they chagned an exisitng value :

<cfloop index="dateCodeID" list="#form.dateCodeID#" delimiters=",">
<cfquery name="qryUpdateDateCodes" datasource="db">
udate table
     set dateCode = '#Evaluate("form.dateCode#dateCodeID#")#'
    where stockScreenID = '#form.stockScreenID#'
and dateCodeID = '#dateCodeID#'
</cfquery>
</cfloop>

and I would use the cfloop list to insert a new value, but how would I know which one to use when they submit ? I do not know whether an exisitng value(s) changed or new value(s) added.

Thanks

    This topic has been closed for replies.

    6 replies

    Inspiring
    June 4, 2009

    Another approach is to insert everything into a working table.  Then do your inserts and updates using sql.

    June 3, 2009

    You can do the insert when necessary and
    always do the update even if it is not necessary.

    <cfloop index="dateCodeID" list="#form.dateCodeID#" delimiters=",">
    <cfquery name="qryInsertDateCodes" datasource="db">
    insert into table a
           (a.stockScreenId,       a.dateCodeID,   a.dateCode)
    values ('#form.stockScreenID#','#dateCodeID#','#Evaluate("form.dateCode#dateCodeID#")# ')
    where  ('#form.stockScreenID#','#dateCodeID#') not in
    (select b.stockScreenId,       b.dateCodeID
    from   table b where b.stockScreenId = '#form.stockScreenID#'
                    and   b.dateCodeID    = '#dateCodeID#'
    )
    </cfquery>
    <cfquery name="qryUpdateDateCodes" datasource="db">
    update table
         set dateCode = '#Evaluate("form.dateCode#dateCodeID#")#'
        where stockScreenID = '#form.stockScreenID#'
    and dateCodeID = '#dateCodeID#'
    </cfquery>
    </cfloop>

    Message was edited by: draves

    trojnfnAuthor
    Inspiring
    June 4, 2009

    Thanks draves for the response and code sample, I think this is exactly what I am looking for.

    One question. If there is no new record to to insert, it would go thru the insert process anyways, and the NOT IN part will fail, so there would be no insert ? Then it will just fall thru and always perform the update even if there were no changes ?

    June 4, 2009

    Yes, only if the data is not in a record will a new record be inserted and the update will always happen.

    Inspiring
    June 2, 2009

    MySQL provides a non-standard REPLACE INTO syntax which glosses over this issue.

    However... I prefer to use stored procedures and views for all database activities these days, mainly because it puts all of the database-related logic and dependencies into the database itself... where I can always find them and can always know what is actually going on.  Put the necessary logic, whatever it is, into the stored procedure.

    BKBK
    Community Expert
    May 29, 2009

    Update or insert is not determined by how you get data into the database table, but by what you wish to get into the table. The main thing to keep your eye on is the table's primary key.

    If a process involves a new primary key, then you must insert a new row. If a process leaves the existing primary keys intact, then you should update.

    Inspiring
    May 28, 2009

    You could put your logic in a stored procedure if your database supports this.

    Psuedo code for logic contained in stored procedure:

    -- try to update any existing records
    UPDATE table
    SET dateCode = value1
    WHERE stockScreenID = value2
        AND dateCodeID = value3

    -- if no records updated, insert a new record
    IF ( COUNT_OF_RECORDS_UPDATED = 0 )
        INSERT INTO table  ( dateCode,  stockScreenID, dateCodeID )
        VALUES ( value1, value2, value3 )

    Alternately you could have your stored procedure check to see if a record matching the the stockScreenID and dateCodeID values exists.  Then either execute an update or insert.

    trojnfnAuthor
    Inspiring
    May 31, 2009

    ok, I think I understand what you are dolng here, but I have a couple of questions.

    So if I have three existing values displayed, and add a new one, would I first loop thru the insert to check for the three existing records, and then insert the new record if no insert is performed ?

    Is the statement IF ( COUNT_OF_RECORDS_UPDATED = 0 ) the actual statement to check for udpated records ?

    What if I update only one of the three existing records, and also add a new record ?

    BKBK
    Community Expert
    June 1, 2009

    The variable that determines whether it's an update or an insert is the form field that corresponds to the primary key of the database table. With each form submission, check to see if that variable already occurs as a primary key in the table. If so, then update the record corresponding to that key. If not, insert a new record.

    Inspiring
    May 27, 2009

    My approach is name the formfields to reflect my intended action.  When creating the update stuff, the fields would be called update#id_of_the_record# or something like that.  Fields for new records would be called something like NewRecord#something#.

    Something else I do for the update part, is to start by putting all the formfield stuff into a cold fusion query.  Then I run a db query followed by a Q of Q to identify which records actually need updating.  No sense running an update query when the data didn't change.