Postgresql PL/PGSQL function

Asked

Viewed 276 times

0

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, however I have read in the official documentation of postgresql that there are other ways to do this function.

What would be the "correct" form, or this form I posted is the "correct"?

Example of the documentation I could adapt to 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;

1 answer

0

The only observation is that it is not necessary plpgsql, simply 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.