March 24th, 2013, 04:29 PM
Add Values to Existing Columns or Insert - Stored Procedure
Hello all, Im new to Firebird, and kind of ripping my hair out, on how best to do this Firebird.
Assume I have a table MyTotals:
** clientID (string)
** tMonth (int)
** is Primary Key
Now I call my stored Proc with the 4 values (clientID, Month, txBytes, and rxBytes)
What I want to happen is if a row exists matching the Primary key, then add my txBytes and rxBytes provided in my SP to the existing values in that row, if a row does not exist, create one, with the values provided to the SP.
What would be the best way to accomplish?
Thanks for any help
Im thinking MERGE would be the appropriate starting step here. Is this accurate, best way to do? Any room for improvement?
merge into MyTotals m using (select rxBytes, txBytes)
on m.clientID = :clientID and m.tMonth = :month
when matched then
update set rxBytes = m.rxBytes + :rxBytes,
txBytes = m.txBytes + :txBytes
when not matched then
insert (clientID,tMonth,txBytes,rxBytes) values (:clientID,:tMonth,:rxBytes,:txBytes)
June 21st, 2013, 03:48 AM
You can try this command:
update or insert into MyTotals (clientID,tMonth,txBytes,rxBytes)
I hope is not too late.
June 25th, 2013, 05:53 AM
This will replace the values in txBytes,rxBytes if the row exists. It will not add the new values to the old values.
Originally Posted by costin_mentor
June 28th, 2013, 01:47 AM
You are right, i didn't see that he wants to add new values to old values.
Originally Posted by mIRCata