mirror of
https://github.com/sasjs/core.git
synced 2026-01-09 02:10:06 +00:00
Fix: Unique Indexes Added
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
@li mp_getconstraints.sas
|
@li mp_getconstraints.sas
|
||||||
|
|
||||||
@param lib libref of the library to create DDL for. Should be assigned.
|
@param lib libref of the library to create DDL for. Should be assigned.
|
||||||
@param ds dataset to create ddl for (optional)
|
@param ds dataset to create ddl for
|
||||||
@param fref= the fileref to which to write the DDL. If not preassigned, will
|
@param fref= the fileref to which to write the DDL. If not preassigned, will
|
||||||
be assigned to TEMP.
|
be assigned to TEMP.
|
||||||
@param flavour= The type of DDL to create (default=SAS). Supported=TSQL
|
@param flavour= The type of DDL to create (default=SAS). Supported=TSQL
|
||||||
@@ -68,35 +68,48 @@ create table _data_ as
|
|||||||
separated by ' '
|
separated by ' '
|
||||||
from &syslast
|
from &syslast
|
||||||
;
|
;
|
||||||
quit;
|
|
||||||
|
create table _data_ as
|
||||||
|
select * from dictionary.indexes
|
||||||
|
where upcase(libname)="%upcase(&libref)"
|
||||||
|
%if %length(&ds)>0 %then %do;
|
||||||
|
and upcase(memname)="%upcase(&ds)"
|
||||||
|
%end;
|
||||||
|
order by idxusage, indxname, indxpos
|
||||||
|
;
|
||||||
|
%local idxinfo; %let idxinfo=&syslast;
|
||||||
|
|
||||||
/* Extract all Primary Key and Unique data constraints */
|
/* Extract all Primary Key and Unique data constraints */
|
||||||
%mp_getconstraints(lib=%upcase(&libref),ds=%upcase(&ds),outds=_data_)
|
%mp_getconstraints(lib=%upcase(&libref),ds=%upcase(&ds),outds=_data_)
|
||||||
%local colconst; %let colconst=&syslast;
|
%local colconst; %let colconst=&syslast;
|
||||||
|
|
||||||
%macro addConst();
|
%macro addConst();
|
||||||
data _null_;
|
%global constraints_used;
|
||||||
length ctype $11;
|
data _null_;
|
||||||
set &colconst (where=(table_name="&curds" and constraint_type in ('PRIMARY','UNIQUE'))) end=last;
|
length ctype $11 constraint_name_orig $256 constraints_used $5000;
|
||||||
file &fref mod;
|
set &colconst (where=(table_name="&curds" and constraint_type in ('PRIMARY','UNIQUE'))) end=last;
|
||||||
by constraint_type constraint_name;
|
file &fref mod;
|
||||||
if upcase(strip(constraint_type)) = 'PRIMARY' then ctype='PRIMARY KEY';
|
by constraint_type constraint_name;
|
||||||
else ctype=strip(constraint_type);
|
retain constraints_used;
|
||||||
%if &flavour=TSQL %then %do;
|
constraint_name_orig=constraint_name;
|
||||||
column_name=catt('[',column_name,']');
|
if upcase(strip(constraint_type)) = 'PRIMARY' then ctype='PRIMARY KEY';
|
||||||
constraint_name=catt('[',constraint_name,']');
|
else ctype=strip(constraint_type);
|
||||||
%end;
|
%if &flavour=TSQL %then %do;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
column_name=catt('[',column_name,']');
|
||||||
column_name=catt('"',column_name,'"');
|
constraint_name=catt('[',constraint_name,']');
|
||||||
constraint_name=catt('"',constraint_name,'"');
|
%end;
|
||||||
%end;
|
if first.constraint_name then do;
|
||||||
if first.constraint_name then do;
|
constraints_used = catx(' ', constraints_used, constraint_name_orig);
|
||||||
put " ,CONSTRAINT " constraint_name ctype "(" ;
|
put " ,CONSTRAINT " constraint_name ctype "(" ;
|
||||||
put ' ' column_name;
|
put ' ' column_name;
|
||||||
end;
|
end;
|
||||||
else put ' ,' column_name;
|
else put ' ,' column_name;
|
||||||
if last.constraint_name then put " )";
|
if last.constraint_name then do;
|
||||||
run;
|
put " )";
|
||||||
|
call symput('constraints_used',strip(constraints_used));
|
||||||
|
end;
|
||||||
|
run;
|
||||||
|
%put &=constraints_used;
|
||||||
%mend;
|
%mend;
|
||||||
|
|
||||||
data _null_;
|
data _null_;
|
||||||
@@ -108,13 +121,13 @@ run;
|
|||||||
%if &flavour=SAS %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
|
put "/* SAS Flavour DDL for %upcase(&libref).&curds */";
|
||||||
put "proc sql;";
|
put "proc sql;";
|
||||||
run;
|
run;
|
||||||
%do x=1 %to %sysfunc(countw(&dsnlist));
|
%do x=1 %to %sysfunc(countw(&dsnlist));
|
||||||
%let curds=%scan(&dsnlist,&x);
|
%let curds=%scan(&dsnlist,&x);
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
if _n_ eq 1 then put "/* SAS Flavour DDL for %upcase(&libref).&curds */";
|
|
||||||
length nm lab $1024;
|
length nm lab $1024;
|
||||||
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
||||||
|
|
||||||
@@ -142,6 +155,25 @@ run;
|
|||||||
file &fref mod;
|
file &fref mod;
|
||||||
put ');';
|
put ');';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
|
/* Create Unique Indexes, but only if they were not already defined within the Constraints section. */
|
||||||
|
data _null_;
|
||||||
|
*length ds $128;
|
||||||
|
set &idxinfo (where=(memname="&curds" and unique='yes' and indxname not in (%sysfunc(tranwrd("&constraints_used",%str( ),%str(","))))));
|
||||||
|
file &fref mod;
|
||||||
|
by idxusage indxname;
|
||||||
|
/* ds=cats(libname,'.',memname); */
|
||||||
|
if first.indxname then do;
|
||||||
|
put 'CREATE UNIQUE INDEX ' indxname "ON &libref..&curds (" ;
|
||||||
|
put ' ' name ;
|
||||||
|
end;
|
||||||
|
else put ' ,' name ;
|
||||||
|
*else put ' ,' name ;
|
||||||
|
if last.indxname then do;
|
||||||
|
put ');';
|
||||||
|
end;
|
||||||
|
run;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ods output IntegrityConstraints=ic;
|
ods output IntegrityConstraints=ic;
|
||||||
proc contents data=testali out2=info;
|
proc contents data=testali out2=info;
|
||||||
@@ -191,6 +223,24 @@ run;
|
|||||||
/* Extra step for data constraints */
|
/* Extra step for data constraints */
|
||||||
%addConst()
|
%addConst()
|
||||||
|
|
||||||
|
/* Create Unique Indexes, but only if they were not already defined within the Constraints section. */
|
||||||
|
data _null_;
|
||||||
|
*length ds $128;
|
||||||
|
set &idxinfo (where=(memname="&curds" and unique='yes' and indxname not in (%sysfunc(tranwrd("&constraints_used",%str( ),%str(","))))));
|
||||||
|
file &fref mod;
|
||||||
|
by idxusage indxname;
|
||||||
|
*ds=cats(libname,'.',memname);
|
||||||
|
if first.indxname then do;
|
||||||
|
/* add nonclustered in case of multiple unique indexes */
|
||||||
|
put ' ,index [' indxname +(-1) '] UNIQUE NONCLUSTERED (';
|
||||||
|
put ' [' name +(-1) ']';
|
||||||
|
end;
|
||||||
|
else put ' ,[' name +(-1) ']';
|
||||||
|
if last.indxname then do;
|
||||||
|
put ' )';
|
||||||
|
end;
|
||||||
|
run;
|
||||||
|
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
put ')';
|
put ')';
|
||||||
@@ -222,9 +272,7 @@ run;
|
|||||||
from dictionary.libnames
|
from dictionary.libnames
|
||||||
where libname="&libref" and engine='POSTGRES';
|
where libname="&libref" and engine='POSTGRES';
|
||||||
%let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
|
%let schema=%sysfunc(coalescec(&schemaactual,&schema,&libref));
|
||||||
data _null_;
|
|
||||||
file &fref mod;
|
|
||||||
put "CREATE SCHEMA &schema;";
|
|
||||||
%do x=1 %to %sysfunc(countw(&dsnlist));
|
%do x=1 %to %sysfunc(countw(&dsnlist));
|
||||||
%let curds=%scan(&dsnlist,&x);
|
%let curds=%scan(&dsnlist,&x);
|
||||||
data _null_;
|
data _null_;
|
||||||
@@ -252,9 +300,7 @@ run;
|
|||||||
else if type='num' then fmt=' DOUBLE PRECISION';
|
else if type='num' then fmt=' DOUBLE PRECISION';
|
||||||
else fmt='VARCHAR('!!cats(length)!!')';
|
else fmt='VARCHAR('!!cats(length)!!')';
|
||||||
if notnull='yes' then notnul=' NOT NULL';
|
if notnull='yes' then notnul=' NOT NULL';
|
||||||
/* quote column names in case they represent reserved words */
|
put name fmt notnul;
|
||||||
name2=quote(trim(name));
|
|
||||||
put name2 fmt notnul;
|
|
||||||
run;
|
run;
|
||||||
|
|
||||||
/* Extra step for data constraints */
|
/* Extra step for data constraints */
|
||||||
@@ -265,6 +311,24 @@ run;
|
|||||||
put ');';
|
put ');';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
|
/* Create Unique Indexes, but only if they were not already defined within the Constraints section. */
|
||||||
|
data _null_;
|
||||||
|
*length ds $128;
|
||||||
|
set &idxinfo (where=(memname="&curds" and unique='yes' and indxname not in (%sysfunc(tranwrd("&constraints_used",%str( ),%str(","))))));
|
||||||
|
file &fref mod;
|
||||||
|
by idxusage indxname;
|
||||||
|
/* ds=cats(libname,'.',memname); */
|
||||||
|
if first.indxname then do;
|
||||||
|
put 'CREATE UNIQUE INDEX ' indxname "ON &schema..&curds (" ;
|
||||||
|
put ' ' name ;
|
||||||
|
end;
|
||||||
|
else put ' ,' name ;
|
||||||
|
*else put ' ,' name ;
|
||||||
|
if last.indxname then do;
|
||||||
|
put ');';
|
||||||
|
end;
|
||||||
|
run;
|
||||||
|
|
||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if &showlog=YES %then %do;
|
%if &showlog=YES %then %do;
|
||||||
|
|||||||
Reference in New Issue
Block a user