Subject Re: [firebird-support] sql to check if a row has been used in related tables?
Author Lucas Franzen
wang960 schrieb:
> Hi,
>
> I have a table called Item with keyfield itemno, and there are many
> tables use the itemno as foreign key, is there a generic way to check
> if a certain itemno already exists in one of related tables? Thanks.
>
>

Well,
a quick and dirty workaround might be:

if all your fields are defined as foreign keys you can do a delete on
the master record and get the AffectedRows. If the result is bigger than
1 or if an error occurs (if one of the foreign keys is not declared as
on DELETE CASCADE) then you can be quite sure there are dependencies...
Do a rollback afterwards if you don't want to delete the record in that
case.

Luc.