Subject | Re: Trigger for a view doesn't work in all circumstances |
---|---|
Author | skoczian |
Post date | 2008-03-11T18:16:17Z |
--- In firebird-support@yahoogroups.com, "Martijn Tonies"
<m.tonies@...> wrote:
SELECT p_id, p_gattung, p_art, r_betrag, r_startdatum, r_abstand,
p_typ, p_einnahme, p_bemerk
FROM posten
LEFT JOIN regelinfo ON p_id = r_posten
and then the trigger source:
SET TERM ^ ;CREATE TRIGGER BA_POSTEN_REGEL FOR POSTEN_REGEL ACTIVE
BEFORE INSERT OR UPDATE OR DELETE POSITION 0
AS
DECLARE newpid BIGINT = 0;
DECLARE oldpid BIGINT = 0;
DECLARE ins_regel INTEGER = 0;
DECLARE del_regel INTEGER = 0;
BEGIN
IF (DELETING) THEN BEGIN
del_regel = 1;
oldpid = OLD.p_id;
DELETE FROM posten WHERE p_id = OLD.p_id;
END
IF (INSERTING) THEN BEGIN
IF (NEW.r_betrag IS NOT NULL) THEN
ins_regel = 1;
INSERT INTO posten (p_gattung, p_art, p_typ, p_einnahme,
p_bemerk)
VALUES (NEW.p_gattung, NEW.p_art, NEW.p_typ,
NEW.p_einnahme, NEW.p_bemerk)
RETURNING p_id INTO :newpid;
END
IF (UPDATING) THEN BEGIN
UPDATE posten SET p_gattung = NEW.p_gattung,
p_art = NEW.p_art, p_typ = NEW.p_typ,
p_einnahme = NEW.p_einnahme,
p_bemerk = NEW.p_bemerk
WHERE p_id = OLD.p_id;
newpid = OLD.p_id;
IF (NEW.r_betrag IS NULL) THEN
del_regel = 1;
ELSE BEGIN
SELECT (1 - COUNT(*)) FROM regelinfo
WHERE r_posten = :newpid INTO :ins_regel;
IF (ins_regel = 0) THEN
UPDATE regelinfo SET r_betrag = NEW.r_betrag,
r_startdatum = NEW.r_startdatum,
r_abstand = NEW.r_abstand
WHERE r_posten = :newpid;
END
END
IF (del_regel = 1) THEN
DELETE FROM regelinfo WHERE r_posten = :oldpid;
IF (ins_regel = 1) THEN
INSERT INTO regelinfo (r_posten, r_betrag, r_startdatum,
r_abstand)
VALUES (:newpid, NEW.r_betrag, NEW.r_startdatum,
NEW.r_abstand);
END^
SET TERM ; ^
Should I give the DDL for the tables involved?
SQL Monitor and use that?
Thank you,
Herta
<m.tonies@...> wrote:
>First the view definition:
> What is the trigger source?
>
SELECT p_id, p_gattung, p_art, r_betrag, r_startdatum, r_abstand,
p_typ, p_einnahme, p_bemerk
FROM posten
LEFT JOIN regelinfo ON p_id = r_posten
and then the trigger source:
SET TERM ^ ;CREATE TRIGGER BA_POSTEN_REGEL FOR POSTEN_REGEL ACTIVE
BEFORE INSERT OR UPDATE OR DELETE POSITION 0
AS
DECLARE newpid BIGINT = 0;
DECLARE oldpid BIGINT = 0;
DECLARE ins_regel INTEGER = 0;
DECLARE del_regel INTEGER = 0;
BEGIN
IF (DELETING) THEN BEGIN
del_regel = 1;
oldpid = OLD.p_id;
DELETE FROM posten WHERE p_id = OLD.p_id;
END
IF (INSERTING) THEN BEGIN
IF (NEW.r_betrag IS NOT NULL) THEN
ins_regel = 1;
INSERT INTO posten (p_gattung, p_art, p_typ, p_einnahme,
p_bemerk)
VALUES (NEW.p_gattung, NEW.p_art, NEW.p_typ,
NEW.p_einnahme, NEW.p_bemerk)
RETURNING p_id INTO :newpid;
END
IF (UPDATING) THEN BEGIN
UPDATE posten SET p_gattung = NEW.p_gattung,
p_art = NEW.p_art, p_typ = NEW.p_typ,
p_einnahme = NEW.p_einnahme,
p_bemerk = NEW.p_bemerk
WHERE p_id = OLD.p_id;
newpid = OLD.p_id;
IF (NEW.r_betrag IS NULL) THEN
del_regel = 1;
ELSE BEGIN
SELECT (1 - COUNT(*)) FROM regelinfo
WHERE r_posten = :newpid INTO :ins_regel;
IF (ins_regel = 0) THEN
UPDATE regelinfo SET r_betrag = NEW.r_betrag,
r_startdatum = NEW.r_startdatum,
r_abstand = NEW.r_abstand
WHERE r_posten = :newpid;
END
END
IF (del_regel = 1) THEN
DELETE FROM regelinfo WHERE r_posten = :oldpid;
IF (ins_regel = 1) THEN
INSERT INTO regelinfo (r_posten, r_betrag, r_startdatum,
r_abstand)
VALUES (:newpid, NEW.r_betrag, NEW.r_startdatum,
NEW.r_abstand);
END^
SET TERM ; ^
Should I give the DDL for the tables involved?
> Does your component library have a SQL Monitor of sorts, you mightTable-component
> be able to use that to view the SQL statement that the
> generates.As far as I can see - no. Perhaps I could search for a stand-alone
SQL Monitor and use that?
Thank you,
Herta