Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 245
Krunal Pandya Avatar asked Dec 14 '25 03:12

Krunal Pandya


1 Answers

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!