Skip to content

Instantly share code, notes, and snippets.

@sebastianwebber
Last active October 19, 2016 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianwebber/c84ce1c2056f409379757e8e20270024 to your computer and use it in GitHub Desktop.
Save sebastianwebber/c84ce1c2056f409379757e8e20270024 to your computer and use it in GitHub Desktop.
exemplo de função `SQL`
-- based on http://pastebin.com/igga50QK
CREATE OR REPLACE FUNCTION fn_busca_valor_bloco_sql(p_blccodigo INTEGER, p_itemcodigo INTEGER, p_cadastro BIGINT)
RETURNS TEXT AS
$BODY$
SELECT
CASE WHEN lsicodigo IS NULL THEN lsidescricao ELSE itmconteudo END
FROM tbblocoitem
LEFT JOIN tblistaitem using(blccodigo ,itemcodigo ,lsicodigo)
WHERE tbblocoitem.blccodigo = p_blccodigo
AND tbblocoitem.itmcodigo = p_itemcodigo
AND tbblocoitem.bcicadastro= p_cadastro
AND itemdatahora = (SELECT MAX(a.itemdatahora)
FROM tbblocoitem a
WHERE (a.blccodigo,a.itemcodigo,a.cadastro)=(tbblocoitem.blccodigo,tbblocoitem.itemcodigo,tbblocoitem.cadastro))
$BODY$
LANGUAGE 'SQL';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment