Passing a record as function argument PL/pgSQL

后端 未结 2 1751
遥遥无期
遥遥无期 2020-12-09 04:40

First I am really new to pl/pgsql. Need it for a project.

I am stuck with this (simplified) problem.

My db schema has a n to m relationship (author, books, a

2条回答
  •  执笔经年
    2020-12-09 04:50

    Passing JSON datatype (Postgresql 9.2 or higher):

    CREATE OR REPLACE FUNCTION f_insert_book(_book json, _authors json)
       RETURNS void AS 
    $$
    BEGIN
    -- insert book into table books
    Insert into books values select * from json_populate_recordset(null:book, _book);
        -- for each author add an entry to author_books table
    Insert into authors values select * from json_populate_recordset(null:authors, _authors);
    end;
    $$ language plpgsql;
    

提交回复
热议问题