🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #11037

Re: Avoid deletion of master with details

#11037
From: Bojidar Alexandrov
Date: Tue, 17 Dec 2002 08:20
67 lines
2784 bytes
Stig Andersen wrote:
> Hi,
>
> How do I avoid that a master record is deleted if it has details (on
> the server side):
> BUT instead of being deleted it should instead be "marked obsolete"
> For example
>
> create MASTER_TABLE
> (
>  MASTER_ID integer not null primary key,
> OBSOLETE smallint /*, ("Boolean")
>  M_Field2 something*/
> );
> create DETAIL_TABLE
> (
> DETAIL_ID integer not null primary key,
> MASTER_ID integer/*,
> D_Field1 something*/
> );
> alter table DETAIL_TABLE add foreign key (MASTER_ID)
>  references MASTER_TABLE(MASTER_ID) on cascade update on delete
> update;
>
> My solution (which is NOT ideal):
> create Exception CANNOT_DELETE 'Sorry, no delete';
> set term ^;
> create trigger MASTER_TABLE_BD for MASTER_TABLE
> active before delete
> as
>  declare variable CNT integer;
>  begin
>   SELECT COUNT(*)
>    from DETAIL_TABLE
>    where DETAIL_ID = OLD.DETAIL_ID
>    into :CNT;
>   if (:CNT > 0) then
>     exception CANNOT_DELETE;
> /* the following does not work like I had imagined:
>  WHEN ANY do
>    begin
>     update MASTER_TABLE
>     set OBSOLETE = 1
>     where MASTER_ID = OLD.MASTER_ID;
>   end */        /*see what I try to do? - but the row gets deleted
> anyway!!*/
>  end^
> set term ;^
>
> I know it can be done in a TIBDataSet, but that is on the client, not
> the Database.
> Any suggestions are much appreciated.
>
> Sincerely
>  Stig Andersen

You raise the error then catch it so nothing happens. If you raise error out
of the trigger may  be it will work but
ugly.
I recommend you to use a view and make triggers on to take appropriate
action on the physical table.


--
Bojidar Alexanrov
Kodar Ltd.
http://www.Kodar.net
Technical Details
Message-ID: <3dfede65@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed-west.nntpserver.com!hub1.meganetnews.com!nntpserver.com!telocity-west!DIRECTV!203.109.225.2!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <3dfe4ecd@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114a0e,f0f7fed5515a9c91
X-Google-Attributes: gid114a0e,public
X-Google-ArrivalTime: 2002-12-17 00:28:29 PST
From: "Bojidar Alexandrov" <bojo@kodar.net>
Newsgroups: borland.public.interbase.sql
References: <3dfe4ecd@newsgroups.borland.com>
Subject: Re: Avoid deletion of master with details
Date: Tue, 17 Dec 2002 10:20:49 +0200
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4920.2300
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4920.2300
NNTP-Posting-Host: 213.169.35.65
Message-ID: <3dfede65@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1040113253 213.169.35.65 (17 Dec 2002 00:20:53 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed-west.nntpserver.com!hub1.meganetnews.com!nntpserver.com!telocity-west!DIRECTV!203.109.225.2!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail