first version of trigger files

This commit is contained in:
harishkrishnaappa
2021-09-20 12:16:02 +05:30
commit 794120b26b
2 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS trigger AS
$$
declare
v_upd_string text;
v_sqlstate text;
v_message text;
v_context text;
BEGIN
v_upd_string := 'UPDATE ' || TG_TABLE_SCHEMA || '.' || TG_TABLE_NAME;
IF (TG_OP = 'INSERT') THEN
v_upd_string := v_upd_string || ' SET create_timestamp = ''' || now() || ''', ';
v_upd_string := v_upd_string || ' update_timestamp = ''' || now() || ''' where syspk = ' || new.syspk;
ELSEIF (TG_OP = 'UPDATE') THEN
v_upd_string := v_upd_string || ' SET update_timestamp = ''' || now() || ''' where syspk = ' || old.syspk;
END IF;
execute (v_upd_string);
RETURN new;
EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS
v_sqlstate = returned_sqlstate,
v_message = message_text,
v_context = pg_exception_context;
RETURN NULL;
END
$$ LANGUAGE plpgsql;