Declaring the tuple structure of a record in PL/pgSQL

前端 未结 3 1232
心在旅途
心在旅途 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:32

    There might be some way that avoids the explicit type declaration, but offhand the best I can come up with is:

    CREATE TYPE my_func_return AS (
        a integer,
        b varchar
      );
    
    CREATE OR REPLACE FUNCTION my_func()
      RETURNS my_func_return AS $$
    DECLARE
      r my_func_return;
    BEGIN
      SELECT 1, 'one' INTO r.a, r.b;
      RETURN r;
    END; $$ LANGUAGE plpgsql;
    

    Oh, I almost forgot the simplest way to do this:

    CREATE OR REPLACE FUNCTION my_func2(out a int, out b text)
      RETURNS RECORD AS $$
    BEGIN
      SELECT 1, 'one' INTO a, b;
      RETURN;
    END; $$ LANGUAGE plpgsql;
    

提交回复
热议问题