Re: Help: update record if existed, else insert it
Posted in 1996
How about twisting things to: 1. insert the new record, whether it already exists or not. 2. if the insert fails, use exception to update the existing record. This of course assumes that you have an appropriate unique constraint on the columns that define "existence". If the usual case is an update, this method might end up being inefficient because you will always be trying an extra insert. You could also use your method of updating first and then using dbinfo (assuming you have online 7.x) to find out whether the update did anything, and inserting if it didn't. Chih-Cherng Chin wrote: > ... > Being new to SPL, and wanting to implement a SP to log hits, I'm having > problems writing it. I want to do the following: > > 1. if the corresponding record exists, update its column, > 2. if not, insert a new record. > > Originally, I thought about doing it this way: > > 1. Try to update the record, whether it exists or not, > 2. if the update failed, use the exception to insert a new record. > > But it turns out that the failed update is not considered as a > error, so I can't do that. I think there are many problems similar > to this, so please share your experience. Thanks in advance. > ... ---------------------------------------------------------------------- John H. Frantz Power-4gl: Extending Informix-4gl frantz@centrum.is http://www.strengur.is/~frantz/pow4gl.html