Dynamic Table Name In Postgresql Trigger

Dynamic table name in PostgreSQL trigger used as “TG_TABLE_NAME”
“TG_TABLE_NAME” is a name of the table that caused the trigger invocation.

Like If you want to copy data from one table to another on updating first table

Then start using triggers

Lets say you have table name “first_table”

Create same table as log table to store log of first table data.

Now using PostgreSQL to create functions and trigger:

Firstly, Create a trigger on first table ON UPDATE EACH Rows like

CREATE TRIGGER trigger_name AFTER DELETE OR UPDATE ON first_table FOR EACH ROW EXECUTE PROCEDURE updation_log()

Now Create Function To Execute This Trigger

BEGIN
EXECUTE format('INSERT INTO %I values(($1).*)', TG_TABLE_NAME ||'log') USING OLD ;
RETURN NEW;
END;

format() is simply a string function like as used in C language sprintf().

This procedure explain that first table date is copied to second table defined as “TG_TABLE_NAME ||’log’ ”
if table name is first_table then data is copied to first_tablelog.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.



nike tn pas cher tn pas cher peuterey outlet piumini peuterey outlet spaccio peuterey outlet peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online hogan outlet scarpe hogan outlet hogan outlet online doudoune moncler pas cher moncler pas cher doudoune moncler hogan outlet scarpe hogan outlet hogan outlet online nike tn pas cher tn pas cher