Board index » delphi » Help with update SQL problem

Help with update SQL problem

I want to update an Interbase table with a record which may or may not have a
 duplicate key to one already in the table. In Paradox I could have the record
 updated, just changing the attribute fields. If it does have a duplicate, I
 would like the new record accepted and the previous discarded. If there is no
 duplicate key, I would like the new record to be added.
Can anyone suggest the most efficient method of doing this?
Thanks
Matt

 

Re:Help with update SQL problem


Quote
>I want to update an Interbase table with a record which may or may not have
a
> duplicate key to one already in the table. In Paradox I could have the
record
> updated, just changing the attribute fields. If it does have a duplicate,
I
> would like the new record accepted and the previous discarded. If there is
no
> duplicate key, I would like the new record to be added.
>Can anyone suggest the most efficient method of doing this?

I'd suggest an SQL query along the lines of:

IF EXISTS ( SELECT 1 FROM mytable WHERE mykeyfield = :foo )
THEN UPDATE mytable SET myfield=:bar WHERE mykeyfield = :foo
ELSE INSERT mytable VALUES ( :foo, :bar )

(this is Sybase SQL but Interbase can't be all that different)
...
hope this helps!

(remove .nospam from address)
St?le Sannerud
Systems Consultant
Telenor Direkte A/S
stale.sannerud.nos...@gjovik.direkte.telenor.no

Other Threads