Subject Re: [firebird-support] Better way to update table?
Author Raymond Kennington
Nigel Weeks wrote:
>
> Use a stored procedure, and don't rely on Primary Key execeptions to do your
> checking...
>
> set term !! ;
> create procedure sp_insert(
> val1 type,
> val2 type,
> ...
> ) returns (
> int_success integer
> ) as begin
> /* Find if it's there (Use primary keys if possible)*/
> select count(*) from table where val1 = :val1, val2 = :val2...
> into :int_success;
>
> /* Test is we've got it */
> if(int_success >0)THEN
> begin
> /* We have something */
> update table set val1 = :val1, val2 = :val2
> where primary key field = :primary key field;
> end
> else
> begin
> /* Nothing was found */
> insert into table (val1, val2)
> values (:val1, :val2);
> end
> /* Send out the int_success(with the 0 or 1 for insert or update)*/
> SUSPEND;
> end !!
> set term ; !!

Better to use EXISTS rather than counting the records.
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)