45%macro mp_ds2ddl(libref,ds,fref=getddl,flavour=SAS,showlog=YES,schema=
50%
if %mf_existfileref(&fref)=0 %then %
do;
54%
if %length(&libref)=0 %then %let libref=WORK;
55%let flavour=%upcase(&flavour);
59 select * from dictionary.tables
60 where upcase(libname)=
"%upcase(&libref)"
62 %
if %length(&ds)>0 %then %
do;
63 and upcase(memname)=
"%upcase(&ds)"
66%local tabinfo; %let tabinfo=&syslast;
69 select * from dictionary.columns
70 where upcase(libname)=
"%upcase(&libref)"
71 %
if %length(&ds)>0 %then %
do;
72 and upcase(memname)=
"%upcase(&ds)"
75%local colinfo; %let colinfo=&syslast;
78 select distinct upcase(memname) into: dsnlist
84 select * from dictionary.indexes
85 where upcase(libname)=
"%upcase(&libref)"
86 %
if %length(&ds)>0 %then %
do;
87 and upcase(memname)=
"%upcase(&ds)"
89 order by idxusage, indxname, indxpos
91%local idxinfo; %let idxinfo=&syslast;
94%mp_getconstraints(lib=%upcase(&libref),ds=%upcase(&ds),outds=_data_)
95%local colconst; %let colconst=&syslast;
97%local constraints_used;
100 length ctype $11 constraint_name_orig $256 constraints_used $5000;
102 where=(table_name=
"&curds" and constraint_type in (
'PRIMARY',
'UNIQUE'))
105 by constraint_type constraint_name;
106 retain constraints_used;
107 constraint_name_orig=constraint_name;
108 if upcase(strip(constraint_type)) = 'PRIMARY' then ctype='PRIMARY KEY';
109 else ctype=strip(constraint_type);
110 %if &flavour=TSQL %then %do;
111 column_name=catt('[',column_name,']');
112 constraint_name=catt('[',constraint_name,']');
114 %else %if &flavour=PGSQL %then %do;
115 column_name=catt('"',column_name,'"');
116 constraint_name=catt('"',constraint_name,'"');
118 if first.constraint_name then do;
119 constraints_used = catx(' ', constraints_used, constraint_name_orig);
120 put " ,CONSTRAINT " constraint_name ctype "(" ;
123 else put ' ,' column_name;
124 if last.constraint_name then do;
126 call symput('constraints_used',strip(constraints_used));
129 %put &=constraints_used;
138%if &flavour=SAS %then %do;
139 %do x=1 %to %sysfunc(countw(&dsnlist));
140 %let curds=%scan(&dsnlist,&x);
148 length lab $1024 typ $20;
149 set &colinfo (where=(upcase(memname)="&curds")) end=last;
152 if memtype='DATA' then do;
153 put "create table &libref..&curds(";
157 put "create view &libref..&curds(";
162 if length(format)>1 then fmt=" format="!!cats(format);
163 if length(label)>1 then
164 lab=" label="!!cats("'",tranwrd(label,"'","''"),"'");
165 if notnull='yes' then notnul=' not null';
166 if type='
char' then typ=cats('
char(',length,')');
167 else if length ne 8 then typ='num length='!!cats(length);
169 put name typ fmt notnul lab;
188 and indxname not in (
189 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
194 by idxusage indxname;
196 if first.indxname then do;
197 put 'CREATE UNIQUE INDEX ' indxname "ON &libref..&curds (" ;
201 *else put ' ,' name ;
202 if last.indxname then do;
214%else %if &flavour=TSQL %then %do;
218 select sysvalue into: schemaactual
219 from dictionary.libnames
220 where upcase(libname)="&libref" and engine='SQLSVR';
221 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
223 %do x=1 %to %sysfunc(countw(&dsnlist));
224 %let curds=%scan(&dsnlist,&x);
230 set &colinfo (where=(upcase(memname)="&curds")) end=last;
232 if memtype='DATA' then do;
233 put "create table [&schema].[&curds](";
237 put "create view [&schema].[&curds](";
242 format=upcase(format);
244 %if &applydttm=YES %then %do;
245 else if format=:'DATETIME' then fmt='[datetime2](7) ';
247 else if type='num' then fmt='[decimal](18,2)';
248 else if length le 8000 then fmt='[varchar]('!!cats(length)!!')';
249 else fmt=cats('[varchar](max)');
250 if notnull='yes' then notnul=' NOT NULL';
251 put "[" name +(-1) "]" fmt notnul;
265 and indxname not in (
266 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
271 by idxusage indxname;
272 *ds=cats(libname,'.',memname);
273 if first.indxname then do;
275 put ' ,index [' indxname +(-1) '] UNIQUE NONCLUSTERED (';
276 put ' [' name +(-1) ']';
278 else put ' ,[' name +(-1) ']';
279 if last.indxname then do;
293 length nm $64 lab $1024;
294 set &colinfo (where=(upcase(memname)="&curds" and label ne '')) end=last;
295 nm=cats("N'",tranwrd(name,"'","''"),"'");
296 lab=cats("N'",tranwrd(label,"'","''"),"'");
298 put "EXEC sys.sp_addextendedproperty ";
299 put " @name=N'MS_Description',@value=" lab ;
300 put " ,@level0type=N'SCHEMA',@level0name=N'&schema' ";
301 put " ,@level1type=N'TABLE',@level1name=N'&curds'";
302 put " ,@level2type=N'COLUMN',@level2name=" nm ;
303 if last then put 'GO';
307%else %if &flavour=PGSQL %then %do;
311 select sysvalue into: schemaactual
312 from dictionary.libnames
313 where upcase(libname)="&libref" and engine='POSTGRES';
314 %let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
317 put "CREATE SCHEMA &schema;";
318 %do x=1 %to %sysfunc(countw(&dsnlist));
319 %let curds=%scan(&dsnlist,&x);
320 %local curdsvarcount;
321 %let curdsvarcount=%mf_getvarcount(&libref..&curds);
322 %if &curdsvarcount>1600 %then %do;
336 set &colinfo (where=(upcase(memname)="&curds")) end=last;
339 if memtype='DATA' then do;
340 put "CREATE TABLE &schema..&curds (";
344 put "CREATE VIEW &schema..&curds (";
349 format=upcase(format);
351 %if &applydttm=YES %then %do;
352 else if format=:'DATETIME' then fmt=' TIMESTAMP ';
354 else if type='num' then fmt=' DOUBLE PRECISION';
355 else fmt='VARCHAR('!!cats(length)!!')';
356 if notnull='yes' then notnul=' NOT NULL';
358 name2=quote(trim(name));
359 put name2 fmt notnul;
378 and indxname not in (
379 %sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
384 by idxusage indxname;
385 if first.indxname then do;
386 put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
387 put ' "' name +(-1) '"' ;
389 else put ' ,"' name +(-1) '"';
390 if last.indxname then do;
397%if %upcase(&showlog)=YES %then %do;