44%macro mp_getddl(libref,ds,fref=getddl,flavour=SAS,showlog=NO,schema=
49%
if %mf_existfileref(&fref)=0 %then %
do;
53%
if %length(&libref)=0 %then %let libref=WORK;
54%let flavour=%upcase(&flavour);
58 select * from dictionary.tables
59 where upcase(libname)=
"%upcase(&libref)"
61 %
if %length(&ds)>0 %then %
do;
62 and upcase(memname)=
"%upcase(&ds)"
65%local tabinfo; %let tabinfo=&syslast;
68 select * from dictionary.columns
69 where upcase(libname)=
"%upcase(&libref)"
70 %
if %length(&ds)>0 %then %
do;
71 and upcase(memname)=
"%upcase(&ds)"
74%local colinfo; %let colinfo=&syslast;
77 select distinct upcase(memname) into: dsnlist
83 select * from dictionary.indexes
84 where upcase(libname)=
"%upcase(&libref)"
85 %
if %length(&ds)>0 %then %
do;
86 and upcase(memname)=
"%upcase(&ds)"
88 order by idxusage, indxname, indxpos
90%local idxinfo; %let idxinfo=&syslast;
93%mp_getconstraints(lib=%upcase(&libref),ds=%upcase(&ds),outds=_data_)
94%local colconst; %let colconst=&syslast;
97 %global constraints_used;
99 length ctype $11 constraint_name_orig $256 constraints_used $5000;
101 where=(table_name=
"&curds" and constraint_type in (
'PRIMARY',
'UNIQUE'))
104 by constraint_type constraint_name;
105 retain constraints_used;
106 constraint_name_orig=constraint_name;
107 if upcase(strip(constraint_type)) = 'PRIMARY' then ctype='PRIMARY KEY';
108 else ctype=strip(constraint_type);
109 %if &flavour=TSQL %then %do;
110 column_name=catt('[',column_name,']');
111 constraint_name=catt('[',constraint_name,']');
113 %else %if &flavour=PGSQL %then %do;
114 column_name=catt('"',column_name,'"');
115 constraint_name=catt('"',constraint_name,'"');
117 if first.constraint_name then do;
118 constraints_used = catx(' ', constraints_used, constraint_name_orig);
119 put " ,CONSTRAINT " constraint_name ctype "(" ;
122 else put ' ,' column_name;
123 if last.constraint_name then do;
125 call symput('constraints_used',strip(constraints_used));
128 %put &=constraints_used;
137%if &flavour=SAS %then %do;
138 %do x=1 %to %sysfunc(countw(&dsnlist));
139 %let curds=%scan(&dsnlist,&x);
147 length lab $1024 typ $20;
148 set &colinfo (where=(upcase(memname)="&curds")) end=last;
151 if memtype='DATA' then do;
152 put "create table &libref..&curds(";
156 put "create view &libref..&curds(";
161 if length(format)>1 then fmt=" format="!!cats(format);
162 if length(label)>1 then
163 lab=" label="!!cats("'",tranwrd(label,"'","''"),"'");
164 if notnull='yes' then notnul=' not null';
165 if type='
char' then typ=cats('
char(',length,')');
166 else if length ne 8 then typ='num length='!!cats(length);
168 put name typ fmt notnul lab;
187 and indxname not in (
188 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
193 by idxusage indxname;
195 if first.indxname then do;
196 put 'CREATE UNIQUE INDEX ' indxname "ON &libref..&curds (" ;
200 *else put ' ,' name ;
201 if last.indxname then do;
213%else %if &flavour=TSQL %then %do;
217 select sysvalue into: schemaactual
218 from dictionary.libnames
219 where upcase(libname)="&libref" and engine='SQLSVR';
220 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
222 %do x=1 %to %sysfunc(countw(&dsnlist));
223 %let curds=%scan(&dsnlist,&x);
229 set &colinfo (where=(upcase(memname)="&curds")) end=last;
231 if memtype='DATA' then do;
232 put "create table [&schema].[&curds](";
236 put "create view [&schema].[&curds](";
241 format=upcase(format);
243 %if &applydttm=YES %then %do;
244 else if format=:'DATETIME' then fmt='[datetime2](7) ';
246 else if type='num' then fmt='[decimal](18,2)';
247 else if length le 8000 then fmt='[varchar]('!!cats(length)!!')';
248 else fmt=cats('[varchar](max)');
249 if notnull='yes' then notnul=' NOT NULL';
250 put "[" name +(-1) "]" fmt notnul;
264 and indxname not in (
265 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
270 by idxusage indxname;
271 *ds=cats(libname,'.',memname);
272 if first.indxname then do;
274 put ' ,index [' indxname +(-1) '] UNIQUE NONCLUSTERED (';
275 put ' [' name +(-1) ']';
277 else put ' ,[' name +(-1) ']';
278 if last.indxname then do;
292 length nm $64 lab $1024;
293 set &colinfo (where=(upcase(memname)="&curds" and label ne '')) end=last;
294 nm=cats("N'",tranwrd(name,"'","''"),"'");
295 lab=cats("N'",tranwrd(label,"'","''"),"'");
297 put "EXEC sys.sp_addextendedproperty ";
298 put " @name=N'MS_Description',@value=" lab ;
299 put " ,@level0type=N'SCHEMA',@level0name=N'&schema' ";
300 put " ,@level1type=N'TABLE',@level1name=N'&curds'";
301 put " ,@level2type=N'COLUMN',@level2name=" nm ;
302 if last then put 'GO';
306%else %if &flavour=PGSQL %then %do;
310 select sysvalue into: schemaactual
311 from dictionary.libnames
312 where upcase(libname)="&libref" and engine='POSTGRES';
313 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
316 put "CREATE SCHEMA &schema;";
317 %do x=1 %to %sysfunc(countw(&dsnlist));
318 %let curds=%scan(&dsnlist,&x);
319 %local curdsvarcount;
320 %let curdsvarcount=%mf_getvarcount(&libref..&curds);
321 %if &curdsvarcount>1600 %then %do;
335 set &colinfo (where=(upcase(memname)="&curds")) end=last;
338 if memtype='DATA' then do;
339 put "CREATE TABLE &schema..&curds (";
343 put "CREATE VIEW &schema..&curds (";
348 format=upcase(format);
350 %if &applydttm=YES %then %do;
351 else if format=:'DATETIME' then fmt=' TIMESTAMP ';
353 else if type='num' then fmt=' DOUBLE PRECISION';
354 else fmt='VARCHAR('!!cats(length)!!')';
355 if notnull='yes' then notnul=' NOT NULL';
357 name2=quote(trim(name));
358 put name2 fmt notnul;
377 and indxname not in (
378 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
383 by idxusage indxname;
384 if first.indxname then do;
385 put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
386 put ' "' name +(-1) '"' ;
388 else put ' ,"' name +(-1) '"';
389 if last.indxname then do;
396%if %upcase(&showlog)=YES %then %do;