insert or update into-> data from another table

    0

    0

    Manuel Antunes

    CREATE FUNCTION on_document_change() RETURNS TRIGGER AS $on_document_change$
      BEGIN
        IF (TG_OP = 'INSERT') THEN
          INSERT INTO documents_changelog(document_id, old_data, new_data) VALUES (new.id, NULL, new.data);
        ELSIF (TG_OP = 'UPDATE') THEN
          INSERT INTO documents_changelog(document_id, old_data, new_data) VALUES (old.id, old.data, new.data);
        ELSIF (TG_OP = 'DELETE') THEN
          INSERT INTO documents_changelog(document_id, old_data, new_data) VALUES (old.id, old.data, NULL);
        END IF;
        RETURN NULL;
      END;
    $on_document_change$ LANGUAGE plpgsql;
      
    CREATE TRIGGER trigger_document_change
    AFTER INSERT OR UPDATE OR DELETE ON documents
    FOR EACH ROW EXECUTE PROCEDURE on_document_change();
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.