Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaulMedeiros/66e7b02688f9900fdb11c1d6ad6e6dd9 to your computer and use it in GitHub Desktop.
Save RaulMedeiros/66e7b02688f9900fdb11c1d6ad6e6dd9 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION query_builder(tab_name text) RETURNS table ( aaa int ) LANGUAGE plpgsql AS $$
DECLARE
built_query varchar;
BEGIN
SELECT 'SELECT (
cast(f."' || STRING_AGG (column_name, '" IS NULL as int) + cast(f."') || '" IS NULL as int)) as sum_nulls from public."'|| tab_name ||'" as f; ' into built_query
from information_schema.columns
where table_name = tab_name
group by table_name;
RETURN query EXECUTE built_query;
END; $$;
SELECT query_builder('table_name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment