La gestion des clés étrangères orphelines est un enjeu de maintenance crucial pour tout administrateur SAS. Ce script cible une anomalie spécifique du modèle objet (Open Metadata Architecture) : les objets ForeignKey qui ont perdu leur lien avec une PartnerUniqueKey. Ces résidus surviennent souvent après des suppressions partielles de tables ou des imports/exports mal maîtrisés, et peuvent générer des erreurs inattendues lors du déploiement de flux ETL ou de rapports.
Type : EXTERNE
Les données ne proviennent pas de tables SAS mais sont lues directement depuis le serveur de métadonnées SAS. La connexion est établie via les options 'metaserver', 'metaport', etc. Le script interroge l'API des métadonnées pour trouver des objets spécifiques.
| 1 | DATA _null_; |
| 2 | /* Initialize variables. */ |
| 3 | LENGTH FK_id PT_id $17 type FK_uri PT_uri $50 PT_engine $64 PT_name $32 FK_name $60; |
| 4 | call missing ( of _character_ ); |
| 5 | retain mode "&MODE"; |
| 6 | |
| 7 | /* Define a query for ForeignKey FKobjects that do not have a PartnerUniqueKey association */ |
| 8 | FK_obj="omsobj:ForeignKey?ForeignKey[not(PartnerUniqueKey/*)]"; |
| 9 | |
| 10 | /* Determine how many foreign key objects meet the criteria */ |
| 11 | FK_Count=metadata_resolve(FK_obj,type,FK_id); |
| 12 | put "NOTE: Found " FK_Count "ForeignKey objects that do not have an associated PartnerUniqueKey"; |
| 13 | |
| 14 | /* If any were found, iterate through each one to get the attributes and associations */ |
| 15 | IF FK_Count > 0 THEN DO i = 1 to FK_Count; |
| 16 | /* Get the URI for the nth ForeignKey found */ |
| 17 | FK_rc=metadata_getnobj(FK_obj,i,FK_uri); |
| 18 | |
| 19 | /* Get the name and ID of the ForeignKey FKobject */ |
| 20 | FK_rc=metadata_getattr(FK_uri,"Name",FK_name); |
| 21 | FK_rc=metadata_getattr(FK_uri,"Id",FK_id); |
| 22 | |
| 23 | /* get the table name and id this ForeignKey is associated with */ |
| 24 | PT_rc=metadata_getnasn(FK_uri,"Table",1,PT_uri); |
| 25 | IF PT_rc > 0 THEN DO; |
| 26 | /* Get the name and ID of the table. */ |
| 27 | PT_rc=metadata_getattr(PT_uri,"SASTableName",PT_name); |
| 28 | PT_rc=metadata_getattr(PT_uri,"Id",PT_id); |
| 29 | END; |
| 30 | |
| 31 | /* REPORT and DELETE */ |
| 32 | put "NOTE: Found foreign key: " FK_name "with id: " FK_id "for table: " PT_name "with id: " PT_id; |
| 33 | IF mode = "DELETE" THEN DO; |
| 34 | FK_rc = metadata_delobj(FK_uri); |
| 35 | IF FK_rc THEN put 'ERROR: metadata_delobj failed with return code ' FK_rc; |
| 36 | ELSE put 'NOTE: Foreign key successfully deleted'; |
| 37 | END; |
| 38 | END; |
| 39 | RUN; |