Search code examples
postgresqlpostgresql-9.3

Postgresql function return multiple select statements


Can any one of you tell me how to approach this:

CREATE OR REPLACE FUNCTION name()
  RETURNS ????? AS
$func$
BEGIN

 SELECT * FROM tbl_a a;

 SELECT * FROM tbl_b b;

END
$func$ LANGUAGE plpgsql;

Both tables have different structures.


Solution

  • You can use cursors but I can hardly imagine why you need such a function.

    CREATE OR REPLACE FUNCTION my_multiselect(refcursor, refcursor) RETURNS VOID AS
      $func$
    BEGIN
      OPEN $1 FOR SELECT * FROM information_schema.routines;
      OPEN $2 FOR SELECT * FROM information_schema.sequences;
    END
    $func$ LANGUAGE plpgsql;
    
    BEGIN;
    SELECT my_multiselect('first_cursor_to_routines', 'second_cursor_to_sequences');
    FETCH ALL IN first_cursor_to_routines;
    FETCH ALL IN second_cursor_to_sequences;
    COMMIT;