Postgres function, getting "query has no destination for result data", don't want to use table result type - postgresql

I am writing a simple postgres function. The result of this function should be a row of the "events" table. I have the following:
create or replace function featured_event() returns setof events as
$$
begin
select events.* from events where featured is true;
end
$$ LANGUAGE plpgsql;
I don't want to hardcode the columns, but instead use the structure from the existing table as the return type.
This is not a duplicate of Function with SQL query has no destination for result data as I do not want to use the table result type.

Use SQL function:
create or replace function featured_event() returns setof events as
$$
select events.* from events where featured is true;
$$ LANGUAGE sql;
In plpgsql you should use return query:
create or replace function featured_event_plpgsql() returns setof events as
$$
begin
return query select events.* from events where featured is true;
end;
$$ LANGUAGE plpgsql;

Related

How to get entire table data or multiple rows returned from a function in PG/PLSQL with pgAdmin 4.2.?

I tried using setof and table. While creating function in pgAdmin 4.2 there is no return type called setof or table. If I create function with setof and table name as a selected return type, it only returns one row of table.
CREATE FUNCTION pgsql_returnrecords() RETURNS SETOF RECORD(name char, city, char, id integer) AS
$BODY$
DECLARE
rec RECORD;
BEGIN
select name,city,id INTO rec from test;
return next rec;
END;
$BODY$ language plpgsql;
I want my function to return table data with all rows and columns.
It's either returns setof record or returns table(....) or setof table_name With returns setof record you have to specify the column names when using the function.
You are also not returning a complete result, because you only fetch a single row, put it into the record and return that. To return a real "set" you need to use return query in PL/pgSQL. But such a function is much better written as a SQL function:
CREATE FUNCTION pgsql_returnrecords()
RETURNS table(name text, city text, id integer)
AS
$BODY$
select name,city,id
from test;
$BODY$
language sql;
If you want to always return a complete row from the table test you can simplify that using returns setof test instead of returns table(..)
CREATE FUNCTION pgsql_returnrecords()
RETURNS setof test
AS
$BODY$
select *
from test;
$BODY$ language sql;
Or, if you insist on PL/pgSQL:
CREATE FUNCTION pgsql_returnrecords()
RETURNS table(name text, city text, id integer)
AS
$BODY$
BEGIN
return query
select name,city,id
from test;
END;
$BODY$
language plpgsql;
In both cases you have to use the function like a table in the FROM clause:
select *
from pgsql_returnrecords() ;

Alias column name in Postgres notify

I am using trigger in Postgres database to call function and send newly inserted row to NodeJs application
CREATE OR REPLACE FUNCTION triggerFunction() RETURNS trigger AS $$
DECLARE
BEGIN
PERFORM pg_notify('tableName', row_to_json(NEW)::text );
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
This returns the whole row in json format. However I need to change one of the column name while this row is returned.
Unfortunately AS keywork doesnt work in the row to json with NEW.COLUMN_NAME AS NEW_COLUMN. How can we achieve the solution for this?
CREATE OR REPLACE FUNCTION triggerFunction() RETURNS trigger AS $$
DECLARE
ret json;
BEGIN
select row_to_json(x) into ret from
(select NEW.abc as def, NEW.jkl, NEW.col3) x;
PERFORM pg_notify('tableName', ret::text );
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

PostgreSQL - Function that returns labels of any ENUM

I am trying to write a "generic" function that will return labels of any enum (in any schema)...
But I am not having much luck because I am not sure what should argument type be...
The goal would be to be able to call function like this
SELECT common.get_enum_labels('public.rainbow_colors');
SELECT common.get_enum_labels('audit.user_actions');
This is what I have so
CREATE OR REPLACE FUNCTION common.get_enum_labels(enum_name regtype)
RETURNS SETOF text AS
$$
BEGIN
EXECUTE format('SELECT unnest(enum_range(NULL::%s))::text AS enum_labels ORDER BY 1', enum_name);
END;
$$
LANGUAGE plpgsql
STABLE
PARALLEL SAFE
;
Any tips would be appreciated
The argument type should be regtype, do not forget to return something from the function
CREATE OR REPLACE FUNCTION get_enum_labels(enum_name regtype)
RETURNS SETOF text AS
$$
BEGIN
RETURN QUERY
EXECUTE format('SELECT unnest(enum_range(NULL::%s))::text ORDER BY 1', enum_name);
END;
$$
LANGUAGE plpgsql
create or replace function get_enum_labels(enum_name regtype)
returns setof text language sql stable
as $$
select enumlabel::text
from pg_enum
where enumtypid = enum_name
order by enumsortorder
$$;

Postgresql return nested set of values in stored procedure

I'm currently doing a test in PostgreSQL using PGTAP.
In order to minimize redundancy in my code I placed duplicate code inside a
function. I have functions that both returns a SETOF TEXT.
CREATE FUNCTION _create_common_test(
this_argument varchar
) RETURNS SETOF TEXT AS $$
DECLARE
RETURN NEXT IS(this_argument, 'i_am_argument1', 'Checking Argument 1');
MORE RETURN NEXT STATEMENTS HERE....
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION test_create_common_test_1() RETURNS SETOF TEXT AS $$
BEGIN
RETURN NEXT _create_common_test('i_am_argument1');
END
$$ LANGUAGE plpgsql;
CREATE FUNCTION test_create_common_test_2() RETURNS SETOF TEXT AS $$
BEGIN
RETURN NEXT _create_common_test('i_am_argument2');
END
$$ LANGUAGE plpgsql;
test_create_common_test_1 and test_create_common_test_2 calls the same function _create_common_test() inside their function varying only in the value passed in the argument.
As for my question, Is it possible to return the returned value of _create_common_test() which is a SETOF TEXT in the functions test_create_common_test_1 and test_create_common_test_2?
I have tried using PERFORM _create_common_test('i_am_argument2'),
CREATE FUNCTION test_create_common_test_2() RETURNS SETOF TEXT AS $$
BEGIN
PERFORM _create_common_test('i_am_argument2');
END
$$ LANGUAGE plpgsql;
but it does not enumerate the results I had inside the _create_common_test().
As correctly answered by #a_horse_with_no_name in the comments:
return query select * from _create_common_test('i_am_argument2');

Postgres Stored procedure in select statement cannot get the datas

I am trying to get the data from the Database use function of select prison();.but i got error .Please advise me.
CREATE OR REPLACE FUNCTION prison() RETURNS refcursor AS $$
DECLARE
ref refcursor;
BEGIN
OPEN ref FOR SELECT round,ben_sc,ben_st FROM prison_issue;
RETURN ref;
END;
$$ LANGUAGE plpgsql;
and calling like this
select prison();
also i tried.but cannot executed the rows.
BEGIN;
SELECT prison();
-- Returns: <unnamed portal 2>
FETCH ALL IN "<unnamed portal 24>";
COMMIT;
There is no need for a PL/pgSQL function for this:
CREATE OR REPLACE FUNCTION prison()
RETURNS setof prison_issue
AS $$
SELECT * FROM prison_issue;
$$ LANGUAGE sql;
You also need to use:
select * from prison();
to retrieve the data, do not use select prison() (which only returns a single record, not multiple rows)
You didn't show us your definition of the table prison_issue if you don't want to return all columns you need something like this:
CREATE OR REPLACE FUNCTION prison()
RETURNS table (round integer, ben_sc text, ben_st text)
AS $$
SELECT SELECT round,ben_sc,ben_st FROM prison_issue;
$$ LANGUAGE sql;
You will need to adjust the part table (round integer, ben_sc text, ben_st text) to match the data type of the columns you select.
Below is an example code.I have assumed the data types.Replace with the real ones.
CREATE OR REPLACE FUNCTION prison() RETURNS TABLE(round numeric,ben_sc character varying,ben_st character varying) AS $$
BEGIN
RETURN QUERY SELECT p.round,p.ben_sc,p.ben_st FROM prison_issue p;
END;
$$ LANGUAGE plpgsql;