🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Avoid deletion of master with details

#11129
From: Stig Andersen
Date: Mon, 16 Dec 2002 22:06
53 lines
2238 bytes
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
Technical Details
Message-ID: <3dfe4ecd@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newspump.sol.net!news.maxwell.syr.edu!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114a0e,f0f7fed5515a9c91,start
X-Google-Attributes: gid114a0e,public
X-Google-ArrivalTime: 2002-12-16 14:10:28 PST
From: "Stig Andersen" <irene.jorgensen@get2net.dk>
Newsgroups: borland.public.interbase.sql
Subject: Avoid deletion of master with details
Date: Mon, 16 Dec 2002 23:06:10 +0100
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 195.47.140.6
Message-ID: <3dfe4ecd@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1040076493 195.47.140.6 (16 Dec 2002 14:08:13 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newspump.sol.net!news.maxwell.syr.edu!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail