Insert, on duplicate update in PostgreSQL?

前端 未结 16 2235
别那么骄傲
别那么骄傲 2020-11-21 04:52

Several months ago I learned from an answer on Stack Overflow how to perform multiple updates at once in MySQL using the following syntax:

INSERT INTO table          


        
16条回答
  •  被撕碎了的回忆
    2020-11-21 05:52

    I use this function merge

    CREATE OR REPLACE FUNCTION merge_tabla(key INT, data TEXT)
      RETURNS void AS
    $BODY$
    BEGIN
        IF EXISTS(SELECT a FROM tabla WHERE a = key)
            THEN
                UPDATE tabla SET b = data WHERE a = key;
            RETURN;
        ELSE
            INSERT INTO tabla(a,b) VALUES (key, data);
            RETURN;
        END IF;
    END;
    $BODY$
    LANGUAGE plpgsql
    

提交回复
热议问题