Pacote de funções - PostgreSQL
Criando o SCHEMA PKG_EMA
CREATE SCHEMA pkg_ema AUTHORIZATION postgres;
RETORNA COLUNA GRADE
-- DROP FUNCTION pkg_ema.retorna_coluna_grade(int4, int4, int4, int4, int4);
CREATE OR REPLACE FUNCTION pkg_ema.retorna_coluna_grade(xidprocesso integer, xidatividade integer, xidformulario integer, xidgrade integer, xidvalor integer)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
declare
leitura varchar(4000);
begin
select
resposta into leitura
from public.crm_processo_grade_valor
where idprocesso = xidprocesso
and idformulario = xidformulario
and idatividade = xidatividade
and idgrade = xidgrade
and idvalor = xidvalor
and idrepeticao = (select max(idrepeticao)
from crm_processo_grade_valor xx
where xx.idatividade = crm_processo_grade_valor.idatividade
and xx.idprocesso = crm_processo_grade_valor.idprocesso
and xx.idformulario = crm_processo_grade_valor.idformulario);
return(trim(leitura));
end;
$function$
;