Run SQL statements in PL/pgSQL only if a row doesn't exist

前端 未结 4 670
说谎
说谎 2020-12-21 18:26

I want to do something like this in a PL/pgSQL function in Postgres 9.6:

INSERT INTO table1 (id, value) VALUES (1, \'a\') ON CONFLICT DO NOTHING;
--- If the          


        
4条回答
  •  北海茫月
    2020-12-21 18:55

    The easiest and reliable way is with the especial variable FOUND, this way:

    INSERT INTO table1 (id, value) values (1, ‘a’) on conflict do nothing;
    

    IF FOUND THEN

    --success

    ELSE

    --failure

    END IF;

    Here is the documentation of diagnosing a statement https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html

提交回复
热议问题