How to change column names returned from below function?
Executing:
select * from get_parameterbased_return_Table('condition2');
Column names should change to (col3 text, col4 text)
Sample table and function:
CREATE TABLE public.parameterbased_return_table (
col1 text NULL,
col2 text NULL,
col3 int4 NULL,
col4 int4 NULL
);
INSERT INTO public.parameterbased_return_table (col1, col2, col3, col4)
VALUES
('A', 'B', 11, 22)
, ('dfdf', 'dfe', 14, 545)
;
CREATE OR REPLACE FUNCTION get_parameterbased_return_Table(condition TEXT)
RETURNS TABLE (col1 text, col2 text) AS
$$
BEGIN
IF condition = 'condition1' THEN
RETURN QUERY SELECT t.col1::text, t.col2::text FROM parameterbased_return_Table t;
ELSIF condition = 'condition2' THEN
RETURN QUERY SELECT t.col3::text, t.col4::text FROM parameterbased_return_Table t;
ELSE
-- Handle other conditions or return an empty result set
RETURN QUERY SELECT NULL::record;
END IF;
END;
$$
LANGUAGE plpgsql;
Postgres (like SQL) is strictly typed. Functions cannot change their return type on the fly - including number, names and types of columns for a function returning a row type.
There are a limited workarounds, but you have to pass the required information with the function call one or the other way.
Polymorphic function to change the whole return type
Here, the trick is to pass the actual return type to the function. See:
Only makes sense for exotic use cases.
That said, here is how you pull off such a stunt:
Call:
fiddle
This also returns original data types. You can really return anything here.
Plain column aliases
Again, you have to provide the information in the call. Keeping your original function, you can use column aliases in the call:
fiddle