sql - Are these two PostgreSQL functions equivalent? (RETURNS TABLE and RETURNS SETOF) -
i dealing 2 postgresql installs @ same time: local environment , real remote server. sadly server has old version (8.3.11) , local environment newer (9.4).
i don't have means update remote server @ moment, converting function runs in 9.4 (it uses returns table
) function should fine in 8.3.11 (it should use returns setof
).
but while local environment function works , gives results, remote 1 yields no result (using same tables!)
so, these 2 equivalent?
newer function local environment:
create or replace function pra2.getgamesondate(date) returns table (game_date date, is_home varchar, is_away varchar) $$ begin return query select g.game_date, p1.team_name plays_at_home, p2.team_name plays_away pra2.game g join pra2.team p1 on g.is_home = p1.team_id join pra2.team p2 on g.is_away = p2.team_id g.game_date = $1; if not found raise exception 'no hay partidos para la fecha %.', $1; end if; return; end $$ language plpgsql;
and here function have modified use setof
create type return_type (game_date date, is_home varchar, is_away varchar); create or replace function pra2.getgamesondate(date) returns setof return_type $$ declare _rec return_type; begin return query select g.game_date, p1.team_name plays_at_home, p2.team_name plays_away pra2.game g join pra2.team p1 on g.is_home = p1.team_id join pra2.team p2 on g.is_away = p2.team_id g.game_date = $1; if not found raise exception 'no hay partidos para la fecha %.', $1; end if; return next _rec; end $$ language plpgsql;
it gives no error message @ all, runs ok, yields no results (it raises exception message), wondering whether there wrongly set in setof
query ...
judging documentation, return query
doesn't set found
in postgresql 8.3. (the relevant documentation postgresql 9.1 @ http://www.postgresql.org/docs/9.1/static/plpgsql-statements.html#plpgsql-statements-diagnostics; corresponding statement not appear in corresponding postgresql 8.3 documentation @ http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#plpgsql-statements-diagnostics.) if not found
check isn't doing want.
to honest, i'm not sure best way accomplish in postgresql 8.3. 1 option write this:
create or replace function pra2.getgamesondate(date) returns setof return_type $$ declare _rec return_type; has_rec boolean; begin has_rec := false; _rec in select g.game_date, p1.team_name plays_at_home, p2.team_name plays_away pra2.game g join pra2.team p1 on g.is_home = p1.team_id join pra2.team p2 on g.is_away = p2.team_id g.game_date = $1 loop has_rec := true; return next _rec; end loop; if not has_rec raise exception 'no hay partidos para la fecha %.', $1; end if; end $$ language plpgsql;
(disclaimer: not tested.)
Comments
Post a Comment