diff --git a/base/mp_getconstraints.sas b/base/mp_getconstraints.sas
index 131a152..2989ed6 100644
--- a/base/mp_getconstraints.sas
+++ b/base/mp_getconstraints.sas
@@ -22,6 +22,8 @@
@param [out] outds= (mp_getconstraints) the output dataset
SAS Macros
+ @li mf_getuniquename.sas
+ @li mp_dropmembers.sas
@version 9.2
@author Allan Bowe
@@ -36,6 +38,27 @@
%let lib=%upcase(&lib);
%let ds=%upcase(&ds);
+/**
+ * Neither dictionary tables nor sashelp provides a constraint order column,
+ * however they DO arrive in the correct order. So, create the col.
+ **/
+%local vw;
+%let vw=%mf_getuniquename(prefix=mp_getconstraints_vw_);
+data &vw /view=&vw;
+ set sashelp.vcncolu;
+ where TABLE_CATALOG="&lib";
+
+ /* use retain approach to reset the constraint order with each constraint */
+ length tmp $1000;
+ retain tmp;
+ drop tmp;
+ if tmp ne catx('|',libref,table_name,constraint_type,constraint_name) then do;
+ constraint_order=1;
+ end;
+ else constraint_order+1;
+ tmp=catx('|',libref, table_name, constraint_type,constraint_name);
+run;
+
/* must use SQL as proc datasets does not support length changes */
proc sql noprint;
create table &outds as
@@ -44,8 +67,9 @@ create table &outds as
,a.constraint_type
,a.constraint_name
,b.column_name
+ ,b.constraint_order
from dictionary.TABLE_CONSTRAINTS a
- left join dictionary.constraint_column_usage b
+ left join &vw b
on upcase(a.TABLE_CATALOG)=upcase(b.TABLE_CATALOG)
and upcase(a.TABLE_NAME)=upcase(b.TABLE_NAME)
and a.constraint_name=b.constraint_name
@@ -58,6 +82,13 @@ create table &outds as
and upcase(a.TABLE_NAME)="&ds"
and upcase(b.TABLE_NAME)="&ds"
%end;
+ order by libref, table_name, constraint_name, constraint_order
;
+/* tidy up */
+%mp_dropmembers(
+ &vw,
+ iftrue=(&mdebug=0)
+)
+
%mend mp_getconstraints;
\ No newline at end of file