47%macro mp_ds2ddl(libref,ds,fref=getddl,flavour=SAS,showlog=YES,schema=
52%
if %mf_existfileref(&fref)=0 %then %
do;
56%
if %length(&libref)=0 %then %let libref=WORK;
57%let flavour=%upcase(&flavour);
61 select * from dictionary.tables
62 where upcase(libname)=
"%upcase(&libref)"
64 %
if %length(&ds)>0 %then %
do;
65 and upcase(memname)=
"%upcase(&ds)"
68%local tabinfo; %let tabinfo=&syslast;
71 select * from dictionary.columns
72 where upcase(libname)=
"%upcase(&libref)"
73 %
if %length(&ds)>0 %then %
do;
74 and upcase(memname)=
"%upcase(&ds)"
77%local colinfo; %let colinfo=&syslast;
80 select distinct upcase(memname) into: dsnlist
86 select * from dictionary.indexes
87 where upcase(libname)=
"%upcase(&libref)"
88 %
if %length(&ds)>0 %then %
do;
89 and upcase(memname)=
"%upcase(&ds)"
91 order by idxusage, indxname, indxpos
93%local idxinfo; %let idxinfo=&syslast;
96%mp_getconstraints(lib=%upcase(&libref),ds=%upcase(&ds),outds=_data_)
97%local colconst; %let colconst=&syslast;
99%local constraints_used;
102 length ctype $11 constraint_name_orig $256 constraints_used $5000;
104 where=(table_name=
"&curds" and constraint_type in (
'PRIMARY',
'UNIQUE'))
107 by constraint_type constraint_name;
108 retain constraints_used;
109 constraint_name_orig=constraint_name;
110 if upcase(strip(constraint_type)) = 'PRIMARY' then ctype='PRIMARY KEY';
111 else ctype=strip(constraint_type);
112 %if &flavour=TSQL %then %do;
113 column_name=catt(
'[',column_name,
']');
114 constraint_name=catt(
'[',constraint_name,
']');
116 %else %if &flavour=PGSQL %then %do;
117 column_name=catt(
'"',column_name,
'"');
118 constraint_name=catt(
'"',constraint_name,
'"');
120 if first.constraint_name then do;
121 constraints_used = catx(
' ', constraints_used, constraint_name_orig);
122 put
" ,CONSTRAINT " constraint_name ctype
"(" ;
125 else put ' ,' column_name;
126 if last.constraint_name then do;
128 call symput('constraints_used',strip(constraints_used));
131 %put &=constraints_used;
136 put
"/* DDL generated by &sysuserid on %sysfunc(datetime(),datetime19.) */";
140%if &flavour=SAS %then %do;
141 %do x=1 %to %sysfunc(countw(&dsnlist));
142 %let curds=%scan(&dsnlist,&x);
145 put
"/* SAS Flavour DDL for %upcase(&libref).&curds */";
150 length lab $1024 typ $20;
151 set &colinfo (where=(upcase(memname)=
"&curds")) end=last;
154 if memtype='DATA' then do;
155 put
"create table &libref..&curds(";
159 put
"create view &libref..&curds(";
164 if length(format)>1 then fmt=
" format="!!cats(format);
165 if length(label)>1 then
166 lab=
" label="!!cats(
"'",tranwrd(label,
"'",
"''"),
"'");
167 if notnull='yes' then notnul=' not null';
168 if type='
char' then typ=cats('
char(',length,
')');
169 else if length ne 8 then typ='num length='!!cats(length);
171 put name typ fmt notnul lab;
190 and indxname not in (
191 %sysfunc(tranwrd(
"&constraints_used",%str( ),%str(
",")))
196 by idxusage indxname;
198 if first.indxname then do;
199 put 'CREATE UNIQUE INDEX ' indxname
"ON &libref..&curds (" ;
204 if last.indxname then do;
216%else %if &flavour=TSQL %then %do;
220 select sysvalue into: schemaactual
221 from dictionary.libnames
222 where upcase(libname)=
"&libref" and engine='SQLSVR';
223 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
225 %do x=1 %to %sysfunc(countw(&dsnlist));
226 %let curds=%scan(&dsnlist,&x);
229 put
"/* TSQL Flavour DDL for &schema..&curds */";
232 set &colinfo (where=(upcase(memname)=
"&curds")) end=last;
234 if memtype='DATA' then do;
235 put
"create table [&schema].[&curds](";
239 put
"create view [&schema].[&curds](";
244 format=upcase(format);
246 %if &applydttm=YES %then %do;
247 else if format=:'DATETIME' then fmt='[datetime2](7) ';
249 else if type='num' then fmt='[decimal](18,2)';
250 else if length le 8000 then fmt='[varchar]('!!cats(length)!!
')';
251 else fmt=cats('[varchar](max)');
252 if notnull='yes' then notnul=' NOT NULL';
253 put
"[" name +(-1)
"]" fmt notnul;
267 and indxname not in (
268 %sysfunc(tranwrd(
"&constraints_used",%str( ),%str(
",")))
273 by idxusage indxname;
275 if first.indxname then do;
277 put ' ,index [' indxname +(-1) '] UNIQUE NONCLUSTERED (';
278 put ' [' name +(-1)
']';
280 else put ' ,[' name +(-1)
']';
281 if last.indxname then do;
295 length nm $64 lab $1024;
296 set &colinfo (where=(upcase(memname)=
"&curds" and label ne '')) end=last;
297 nm=cats(
"N'",tranwrd(name,
"'",
"''"),
"'");
298 lab=cats(
"N'",tranwrd(label,
"'",
"''"),
"'");
300 put
"EXEC sys.sp_addextendedproperty ";
301 put
" @name=N'MS_Description',@value=" lab ;
302 put
" ,@level0type=N'SCHEMA',@level0name=N'&schema' ";
303 put
" ,@level1type=N'TABLE',@level1name=N'&curds'";
304 put
" ,@level2type=N'COLUMN',@level2name=" nm ;
305 if last then put 'GO';
309%else %if &flavour=PGSQL %then %do;
313 select sysvalue into: schemaactual
314 from dictionary.libnames
315 where upcase(libname)=
"&libref" and engine='POSTGRES';
316 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
319 put
"CREATE SCHEMA &schema;";
320 %do x=1 %to %sysfunc(countw(&dsnlist));
321 %let curds=%scan(&dsnlist,&x);
322 %local curdsvarcount;
323 %let curdsvarcount=%mf_getvarcount(&libref..&curds);
324 %if &curdsvarcount>1600 %then %do;
327 put
"/* &libref..&curds contains &curdsvarcount vars */";
328 put
"/* Postgres cannot create tables with over 1600 vars */";
329 put
"/* No DDL will be generated for this table";
335 put
"/* Postgres Flavour DDL for &schema..&curds */";
338 set &colinfo (where=(upcase(memname)=
"&curds")) end=last;
341 if memtype='DATA' then do;
342 put
"CREATE TABLE &schema..&curds (";
346 put
"CREATE VIEW &schema..&curds (";
351 format=upcase(format);
353 %if &applydttm=YES %then %do;
354 else if format=:'DATETIME' then fmt=' TIMESTAMP ';
356 else if type='num' then fmt=' DOUBLE PRECISION';
357 else fmt='VARCHAR('!!cats(length)!!
')';
358 if notnull='yes' then notnul=' NOT NULL';
360 name2=quote(trim(name));
361 put name2 fmt notnul;
380 and indxname not in (
381 %sysfunc(tranwrd(
"&constraints_used",%str( ),%str(
",")))
386 by idxusage indxname;
387 if first.indxname then do;
388 put 'CREATE UNIQUE INDEX
"' indxname +(-1) '" '
"ON &schema..&curds(";
389 put '
"' name +(-1) '"' ;
391 else put ' ,
"' name +(-1) '"';
392 if last.indxname then do;
399%if %upcase(&showlog)=YES %then %do;