-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
v_generate_schema_ddl.sql
30 lines (30 loc) · 1.05 KB
/
v_generate_schema_ddl.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--DROP VIEW admin.v_generate_schema_ddl;
/**********************************************************************************************
Purpose: View to get the DDL for schemas.
History:
2014-02-11 jjschmit Created
2018-01-15 pvbouwel Add QUOTE_IDENT for namespace literal
2018-03-30 burck1 Add logic to add AUTHORIZATION clause
Notes:
If you receive the error
[Amazon](500310) Invalid operation: cannot change data type of view column "ddl";
then you must drop the view and re-create it using
DROP VIEW admin.v_generate_schema_ddl;
**********************************************************************************************/
CREATE OR REPLACE VIEW admin.v_generate_schema_ddl
AS
SELECT
nspname AS schemaname,
'CREATE SCHEMA ' + QUOTE_IDENT(nspname) +
CASE
WHEN nspowner > 100
THEN ' AUTHORIZATION ' + QUOTE_IDENT(pg_user.usename)
ELSE ''
END
+ ';' AS ddl
FROM pg_catalog.pg_namespace as pg_namespace
LEFT OUTER JOIN pg_catalog.pg_user pg_user
ON pg_namespace.nspowner=pg_user.usesysid
WHERE nspowner >= 100
ORDER BY nspname
;