Skip to content

Instantly share code, notes, and snippets.

@happysundar
Created October 19, 2014 18:42
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 happysundar/79336ddd05da199450db to your computer and use it in GitHub Desktop.
Save happysundar/79336ddd05da199450db to your computer and use it in GitHub Desktop.
Get all the column names of a table...
DROP FUNCTION IF EXISTS get_column_names_csv( TEXT, TEXT ) CASCADE;
CREATE OR REPLACE FUNCTION
get_column_names_csv(input_schema_name TEXT, input_table_name TEXT)
RETURNS SETOF TEXT IMMUTABLE
AS $$
BEGIN
RETURN QUERY
WITH T1 AS (
SELECT column_name
FROM rovi.information_schema.columns
WHERE
table_name = lower(input_table_name) AND table_schema = lower(input_schema_name)
AND NOT (ordinal_position = 1 AND column_default NOTNULL)
)
SELECT string_agg(column_name, ',')
FROM T1;
END
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment