You are on page 1of 2

Exhibit 5 Trigger Translation Example

8/13/15, 8:49 PM

Exhibit 5 Trigger Translation Example


Category: PL/SQL Translation Tool
Published Date

Oracle trigger translation:


create or replace trigger auditArtists
before insert or update or delete on artists
REFERENCING NEW AS dChgRec
for each row
declare
v_id pls_integer := 0;
begin
dbms_output.put_line('*** auditArtists begin ');
if inserting or updating or deleting then
UPDATE artist_change_log
SET changes = changes + 1
WHERE id = 1;
if (SQL%NOTFOUND) then
dbms_output.put_line('*** auditArtists do first time init of artist_change_log.');
INSERT INTO artist_change_log VALUES (1, 1, NULL, NULL);
end if;
end if;
SELECT max(id)+1 INTO v_id FROM artist_change_log;
if (inserting or (updating and :dChgRec.lastName != :OLD.lastName)) then
dbms_output.put_line('*** auditArtists processing last name ' || :dChgRec.lastName);
if (inserting) then
INSERT INTO artist_change_log VALUES (v_id, 1, user, 'INSERT');
else
INSERT INTO artist_change_log VALUES (v_id, 1, user, 'UPDATE');
end if;
elsif DELETING then
INSERT INTO artist_change_log VALUES (v_id, 1, user, 'DELETE');
end if;
dbms_output.put_line('*** auditArtists exit');
end;

Postgres as translated by magicHat


-- magicHat translation v2.1 July 30, 2014
SET search_path = country, public;
-DROP TRIGGER if exists auditArtists ON artists;
--- Begin trigger function generated from PL/SQL extracted from the Oracle trigger.
-CREATE or REPLACE FUNCTION f_auditArtists()
RETURNS trigger AS $body$
-DECLARE
v_id
integer := 0;
BEGIN
RAISE NOTICE '*** auditArtists begin ';
if TG_OP = 'INSERT' or TG_OP = 'UPDATE' or TG_OP = 'DELETE' then
UPDATE artist_change_log
SET changes = changes + 1
WHERE id = 1;
if (NOT FOUND) then
http://postgresmigrations.com/database-migration/stored-procedure-xhibit-5-trigger-translation-example?tmpl=component&print=1&page=

Page 1 of 2

Exhibit 5 Trigger Translation Example

8/13/15, 8:49 PM

RAISE NOTICE '*** auditArtists do first time init of artist_change_log.';


INSERT INTO artist_change_log VALUES (1, 1, NULL, NULL);
end if;
end if;
SELECT max(id) + 1
INTO v_id
FROM artist_change_log;
if (TG_OP = 'INSERT' or (TG_OP = 'UPDATE' and NEW.lastName != OLD.lastName)) then
RAISE NOTICE '*** auditArtists processing last name %', NEW.lastName;
if ( TG_OP = 'INSERT') then
INSERT INTO artist_change_log
VALUES (v_id, 1, user, 'INSERT');
else
INSERT INTO artist_change_log
VALUES (v_id, 1, user, 'UPDATE');
end if;
elsif TG_OP = 'DELETE' then
INSERT INTO artist_change_log
VALUES (v_id, 1, user, 'DELETE');
end if;
RAISE NOTICE '*** auditArtists exit';
- This is a function that returns type TRIGGER, hence it must return OLD if DELETE, otherwise NEW
if (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') then
RETURN NEW; -- INSERT or UPDATE
else
RETURN OLD; -- DELETE
end if; -- end trigger translation addition
end;
$body$ LANGUAGE plpgsql SECURITY DEFINER;
-create trigger auditArtists
before insert or update or delete on artists
for each row
EXECUTE PROCEDURE f_auditArtists();
-- END auditArtists

http://postgresmigrations.com/database-migration/stored-procedure-xhibit-5-trigger-translation-example?tmpl=component&print=1&page=

Page 2 of 2

You might also like