You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ClinicalTrialsDataProcessing/Scripts/Extracting Tables Views and...

38 lines
1.2 KiB
SQL

SELECT
'CREATE OR REPLACE VIEW ' || schemaname || '.' || viewname || ' AS ' || definition
FROM pg_views
WHERE schemaname != 'pg_catalog'
and schemaname != 'information_schema' -- Replace with your schema name
;
SELECT
'CREATE OR REPLACE MATERIALIZED VIEW ' || schemaname || '.' || viewname || ' AS ' || definition
FROM pg_views
WHERE schemaname != 'pg_catalog'
and schemaname != 'information_schema'
;
SELECT
'CREATE TABLE ' || schemaname || '.' || tablename || E'\n(\n' ||
string_agg(column_definition, E',\n') || E'\n);\n'
FROM (
SELECT
schemaname,
tablename,
column_name || ' ' || data_type ||
CASE
WHEN character_maximum_length IS NOT NULL THEN '(' || character_maximum_length || ')'
ELSE ''
END ||
CASE
WHEN is_nullable = 'NO' THEN ' NOT NULL'
ELSE ''
END as column_definition
FROM pg_catalog.pg_tables t
JOIN information_schema.columns c
ON t.schemaname = c.table_schema
AND t.tablename = c.table_name
WHERE schemaname != 'pg_catalog'
and schemaname != 'information_schema'-- Replace with your schema name
) t
GROUP BY schemaname, tablename;