Return multiple fields as a record in PostgreSQL with PL/pgSQL

前端 未结 6 1369
生来不讨喜
生来不讨喜 2020-11-27 11:57

I am writing a SP, using PL/pgSQL.
I want to return a record, comprised of fields from several different tables. Could look something like this:

CREATE O         


        
6条回答
  •  萌比男神i
    2020-11-27 12:33

    You can achieve this by using simply as a returns set of records using return query.

    CREATE OR REPLACE FUNCTION schemaName.get_two_users_from_school(schoolid bigint)
     RETURNS SETOF record
     LANGUAGE plpgsql
    AS $function$
    begin
    
     return query
      SELECT id, name FROM schemaName.user where school_id = schoolid;
    
    end;
    $function$
    

    And call this function as : select * from schemaName.get_two_users_from_school(schoolid) as x(a bigint, b varchar);

提交回复
热议问题