Create or replace trigger postgres

后端 未结 5 650
情话喂你
情话喂你 2021-02-02 05:01

I want to \"create or replace\" a trigger for a postgres table. However, there is not such sql expression.

I see that I can do a \"DROP TRIGGER IF EXISTS\"

5条回答
  •  难免孤独
    2021-02-02 05:52

    You can combine CREATE OR REPLACE FUNCTION trigger_function with the following script in your SQL:

    DO $$
    BEGIN
      IF NOT EXISTS(SELECT *
        FROM information_schema.triggers
        WHERE event_object_table = 'table_name'
        AND trigger_name = 'trigger_name'
      )
      THEN
        CREATE TRIGGER trigger_name AFTER INSERT ON table_name FOR EACH ROW EXECUTE PROCEDURE trigger_function();
      END IF;
    END;
    $$
    

提交回复
热议问题