Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit from plpgsql function while returning query

In a plpgsql function how can I return a query and return from the function itself? If I just do return query select ... the statement after gets executed as well, so the return doesn't actually return from the whole function?

like image 791
user779159 Avatar asked Jul 26 '26 22:07

user779159


1 Answers

Use just return; as a single statement, example:

create or replace function my_func()
returns setof int language plpgsql as $$
begin
    return query select generate_series(1,2);
    return;
    return query select generate_series(3,4);
end $$;

select my_func();

 my_func 
---------
       1
       2
(2 rows)
like image 59
klin Avatar answered Jul 30 '26 10:07

klin



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!