DROP TRIGGER using a variable to define the trigger name

I have two environments, each with a different trigger name, and I want to use the same script on both envs to remove the trigger I have ...

The problem is that I am doing the following:

variable := 'trigger_name';

DROP TRIGGER variable ON my_table;

      

it tries to DROP run with the name "variable" and not "trigger_name" as I expected ... How can I do this?

Thank you in advance! -BJ

+3


source to share


1 answer


You have to use dynamic SQL with EXECUTE

. You are already using PL / PgSQL, so just:



variable := 'trigger_name';

EXECUTE format('DROP TRIGGER %I ON my_table', variable);

      

+3


source







All Articles