PL / PGSQL function Postgresql

1

I have the following function:

create function sppreenchecombomunicipio(p_uf text) returns TABLE(MUNICIPIO 
text, COD_MUNICIPIO int) 
AS $$
begin 
return query select tb_municipio.municipio, tb_municipio.cod_municipio  from tb_municipio  where    UF = p_UF; end; 
$$LANGUAGE plpgsql;

It is working, but I have read in the official postgresql documentation that other ways exist to do this function.

What would be the "correct" form, or was this form "correct"?

Example of the documentation that I could adapt for my case:

CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo 
AS $BODY$ 
DECLARE 
r foo%rowtype;
BEGIN 
FOR r IN SELECT * FROM foo WHERE fooid > 0 LOOP
    -- can do some processing here
    RETURN NEXT r; -- return current row of SELECT
END LOOP;
RETURN;
END
$BODY$
LANGUAGE plpgsql;
    
asked by anonymous 12.04.2017 / 16:01

1 answer

0

The only observation is that plpgsql is not necessary, just sql:

create function sppreenchecombomunicipio (p_uf text)
returns table (
    municipio text,
    cod_municipio int
) as $$
    select tb_municipio.municipio, tb_municipio.cod_municipio
    from tb_municipio
    where uf = p_uf;
$$ language sql;
    
18.04.2017 / 19:47