Postgres Stored procedure in select statement cannot get the datas - postgresql

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;

Related

Return table in stored procedure postgresql

I want to return the whole table with stored procedure not function
my code is:
CREATE OR REPLACE PROCEDURE public.test()
RETURN TABLE (
id numeric
, test varchar
) AS -- Results -- remove this
$func$
BEGIN
RETURN QUERY
SELECT *
FROM public.test
END
$func$ LANGUAGE plpgsql;
but its not works.
Stored procedures aren't meant to return anything, use a function. And you don't need PL/pgSQL for that either:
create or replace FUNCTION public.test()
returns TABLE (id numeric, test varchar)
AS
$func$
SELECT *
FROM public.test;
$func$
LANGUAGE sql;
As you return all columns of one table, you can also use returns setof
create or replace FUNCTION public.test()
returns setof public.test
AS
$func$
SELECT *
FROM public.test;
$func$
LANGUAGE sql;
Then use it like a table:
select *
from test();

Query returned successfully but i dont see anyway

I'm new to postgresql, i have a procedure block this is working but i dont see any table or column, what this problem.
CREATE PROCEDURE list3(
)
LANGUAGE sql
AS $$
select * from stok
$$;
call list3()
Functions return values.
Example:
create table table_example(col1 varchar(100),col2 int);
CREATE OR REPLACE FUNCTION get_table_example()
RETURNS TABLE (
l_col1 VARCHAR,
l_col2 INT
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT *
FROM table_example;
END;
$$
select * from get_table_example();

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() ;

PostgreSQL Function Type reuse

I have one function, that returns a table. I need another function to inherit same output type, like here:
CREATE OR REPLACE FUNCTION t_report (pMedia text[])
RETURNS TABLE (
media text) AS
$func$
DECLARE
sql text;
BEGIN
sql := 'SELECT
tStreams.Media
FROM
(SELECT
''Audio'' as Media
UNION ALL
SELECT
''Video'' as Media
UNION ALL
SELECT
''App Sharing'' as Media) tStreams
WHERE tStreams.Media in (select unnest($1))';
RETURN QUERY EXECUTE sql
USING pMedia
;
END
$func$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION t_report (pMedia text)
RETURNS TABLE (
media text) AS
$func$
BEGIN
RETURN QUERY EXECUTE 'SELECT * FROM t_report(ARRAY[$1])' USING pMedia
;
END
$func$ LANGUAGE plpgsql;
SELECT *
FROM t_report('Audio');
This works fine, but I need the second function to inherit type from the first.
Is it possible to do straight or I need to create specific type and put it in as RETURNS SETOF MyType?

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

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;