Skip to content

Instantly share code, notes, and snippets.

@RaulMedeiros
Created July 31, 2020 13:32
Show Gist options
  • Save RaulMedeiros/5399a4aa005883494828db9410a02162 to your computer and use it in GitHub Desktop.
Save RaulMedeiros/5399a4aa005883494828db9410a02162 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION qtd(nomeTabela text) RETURNS integer LANGUAGE plpgsql AS $$
DECLARE
resultado integer;
BEGIN
EXECUTE 'SELECT count(1) FROM ' || nomeTabela INTO resultado;
RETURN resultado;
END; $$;
Uso da função:
SELECT qtd('tabela_estoque');
SELECT qtd('tabela_produtos');
Outro uso exemplo:
CREATE OR REPLACE FUNCTION dropConstraint(nomeTabela text, nomeConstraint text) RETURNS void LANGUAGE plpgsql AS $$
BEGIN
EXECUTE 'ALTER TABLE ' || nomeTabela || ' DROP CONSTRAINT ' || nomeConstraint;
END; $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment