0

I know that a table can be return from a postgresql function as below,

CREATE OR REPLACE FUNCTION public.tmp()
 RETURNS table(num int, name text)
 LANGUAGE plpgsql
AS $function$
DECLARE rslt record;
begin
for rslt in select * from t1 loop num:=rslt.num; name:=rslt.name; return next; end loop;        
return ; end;$function$;

But I need to return 2 different tables as optional, that is any one at a execution based on the condition.

For Example: I have 2 table structures.

table(num int, name text)
table(num int, name text, value text)

I need any one of these table structure to return.

How this can be done?

  • 1
    Possible duplicate of [Function to return dynamic set of columns for given table](http://stackoverflow.com/questions/41644680/function-to-return-dynamic-set-of-columns-for-given-table) – Vao Tsun May 03 '17 at 09:30
  • Unrelated, but: you don't need a loop for this. You can use `return query instead` or even just a simple SQL function. –  May 04 '17 at 06:58

1 Answers1

0

You can probably also create two different data types of columns you need and pass into record type. Then you return

Create type datatype as (column type, column type );

Harini
  • 551
  • 5
  • 18