Skip to content

Instantly share code, notes, and snippets.

@sebawebber
Created July 30, 2016 21:51
Show Gist options
  • Select an option

  • Save sebawebber/1b9b588ba2115fe3f47147678c89895a to your computer and use it in GitHub Desktop.

Select an option

Save sebawebber/1b9b588ba2115fe3f47147678c89895a to your computer and use it in GitHub Desktop.
-- criação da função
CREATE OR REPLACE FUNCTION fnc_roda_selects()
RETURNS SETOF RECORD
AS $$
DECLARE
rResult RECORD;
BEGIN
-- tenta buscar dados na tabela com um filtro especifico
PERFORM * FROM dw.dim_tempo;
IF FOUND THEN
FOR rResult IN
SELECT
INTO rResult
*
FROM dw.tabela1
-- COLOCAR FILTROS AQUI
LOOP
RETURN NEXT rResult;
END LOOP;
ELSE
FOR rResult IN
SELECT
INTO rResult
*
FROM dw.tabela_dois
-- COLOCAR FILTROS AQUI
LOOP
RETURN NEXT rResult;
END LOOP;
END IF;
END;
$$ LANGUAGE 'plpgsql';
-- Exemplo de chamada
SELECT * FROM fnc_roda_selects() AS (col1 TEXT, col2 NUMERIC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment