Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Articles
Recherche: 

Rebuilding your Index Tags from scratch without losing anything
Christian Berrigan, January 1, 2001
Rebuilding your Index Tags from scratch without losing your Primery Key, Candidate Keys, or your Persistent Relations Rebuilding a .cdx from scratch (DELETE TAG ALL, then rebuilding each tag) will delete all your persistent relationships in the .dbc, and if you haven't stored your PRIMARY and CA...
Rebuilding your Index Tags from scratch without losing your Primery Key, Candidate Keys, or your Persistent Relations

Rebuilding a .cdx from scratch (DELETE TAG ALL, then rebuilding each tag) will delete all your persistent relationships in the .dbc, and if you haven't stored your PRIMARY and CANDIDATE index metadata, as well as testing for it when rebuilding your index tags, all your index tags will be type 'Regular'.

At first, it's easy to just think we'll rebuild index tags without deleting them first. But sooner or later, we know we will have to rebuild them from scratch. Here's some ideas about how to solve this challenge.

*********************************
** Assumes you already have a table of metadata, including
** index tag names, index expressions and if
** an index is a primary or candidate index
*********************************
** BEFORE YOU DELETE TAGS AND REINDEX, CAPTURE THE RELATIONS
** IN THE .DBC SO YOU CAN RESTORE THEM LATER.
USE myDatabase.DBC AGAIN SHARED IN 0 ALIAS zDBC
SELECT *, RECNO() AS 'RECORDNO' ;
        FROM zDBC ;
        WHERE UPPER(ALLTRIM(objecttype)) == 'RELATION' ;
                AND !DELETED() ;
        INTO CURSOR zRels
******************************************
** PUT YOUR REINDEXING METHOD HERE AND INCLUDE THE FOLLOWING CODE:
******************************************
******************************************
**      aIndexExpr is an array SELECTed from the metadata file where
**              Column1 = TAG NAME [from TAG()]
**              Column2 = INDEX EXPRESSION [from SYS(14) and SYS(2021)]
**                      plus a ## tag indicating if the index is a
**                      ##CANDIDATE## [from CANDIDATE()]
**                      or ##PRIMARY## key [from PRIMARY()]
**
**      lcTable is already USED EXCLUSIVELY and SELECTed
**
DELETE TAG ALL
FOR x=1 TO ALEN(aIndexExpr,1)                   && rebuild each tag
  m.cIndExpr = ALLTRIM(aIndexExpr[x,2])
  DO CASE
    CASE '##PRIMARY##' $ m.cIndExpr
      m.cIndExpr = STRTRAN(m.cIndExpr,'##PRIMARY##','')
      lcExpr = (ALLTRIM(aIndexExpr[x,1]))
      ALTER TABLE (lcTable) ;
                ADD PRIMARY KEY &lcExpr TAG (ALLTRIM(aIndexExpr[x,1]))
    CASE '##CANDIDATE##' $ m.cIndExpr
      m.cIndExpr = STRTRAN(m.cIndExpr,'##CANDIDATE##','')
      &cIndExpr CANDIDATE
    OTHERWISE
      &cIndExpr
  ENDCASE
ENDFOR
******************************************
** FINISH YOUR REINDEXING METHOD HERE
******************************************
** now recall the deleted relations in the dbc
** (this example assumes SET DELETED ON)
SELECT zRels
SET DELETED OFF
** .dbc was already opened ALIAS zDBC earlier
SCAN
  GO (zRels.RecordNo) IN zDBC
  IF DELETED('zDBC')
    SELECT ('zDBC')
    RECALL NEXT 1
    SELECT zRels
  ENDIF
ENDSCAN
USE IN zDBC
USE IN zRels
SET DELETED ON
*********************************
** end
*********************************
That does it! The table(s) in the DBC have had their CDX rebuilt from scratch, and they all still have their PRIMARY and CANDIDATE keys, and all their persistent relations. I haven't tested it, but believe this will also preserve the associated Relational Integrity code and properties.
Christian Berrigan, Enterprise Data Solutions, Inc.