Declaring the tuple structure of a record in PL/pgSQL

前端 未结 3 1223
心在旅途
心在旅途 2020-12-10 21:02

I can\'t find anything in the PostgreSQL documentation that shows how to declare a record, or row, while declaring the tuple structure at the same time. If you don\'t define

3条回答
  •  一生所求
    2020-12-10 21:44

    It is much easier to use OUT parameters rather than a record. If iteratively building a set of records (a table) use RETURN NEXT. If generating from a query, use RETURN QUERY. See:

    https://stackoverflow.com/a/955289/398670

    and:

    http://www.postgresql.org/docs/current/static/plpgsql-declarations.html http://www.postgresql.org/docs/current/static/sql-createfunction.html http://www.postgresonline.com/journal/archives/129-Use-of-OUT-and-INOUT-Parameters.html

    Think:

    CREATE OR REPLACE FUNCTION my_func(OUT a integer, OUT b varchar) RETURNS SETOF RECORD AS $$
    BEGIN
        -- Assign a and b, RETURN NEXT, repeat. when done, RETURN.
    END;
    $$ LANGUAGE 'plpgsql';  
    

提交回复
热议问题