0

I will using simple case condition to insert or update on Postgresql but I didnt know why when i insert insert/update for each condition I got error..

This is my simple function:

CREATE OR REPLACE FUNCTION insert_new_table_log() RETURNS TRIGGER AS $new_table$
BEGIN
SELECT id_hdr,
    CASE id_hdr
        WHEN id_hdr = OLD.id
            THEN 
            (UPDATE new_table_dtl 
             SET id_hdr = OLD.id, nama = OLD.nama,
                 description=OLD.description 
             WHERE id_hdr = OLD.id)
        ELSE
            (INSERT INTO new_table_dtl(id_hdr, nama, description) 
             values(OLD.id, OLD.nama, OLD.description))
    END
    FROM new_table_dtl;
RETURN NEW; 
END;
$new_table$ LANGUAGE plpgsql;

and this is message error

ERROR: syntax error at or near "new_table_dtl" LINE 7: (UPDATE new_table_dtl SET id_hdr = OLD.id, nama = OLD.na...

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
harimu
  • 1
  • 1
  • 1
  • 3
  • 2
    Looks like you are looking for 'Merge' - i.e. if exists, update , else insert. Refer this Stack Overflow discussion - http://stackoverflow.com/questions/17267417/how-do-i-do-an-upsert-merge-insert-on-duplicate-update-in-postgresql – Jayadevan Apr 23 '15 at 03:01
  • http://stackoverflow.com/q/1109061/330315 –  Dec 14 '15 at 07:19

0 Answers0