Previous Topic: Define the Sequence for Reading

Next Topic: Unload Subsets Using Sparse Keys

Unload the Data

You are now in a position to unload all the data in the NDB.

First, write a header record for the data (type 20).

Write out each record as follows:

At the end of all data records, a type 29 record indicates the end of the data. This record contains the count of the number of logical records unloaded.

To protect the unloaded data from the current language (and therefore date) settings, issue an &NDBCTL DATEFMT=NO to force dates to be returned in YYMMDD format:

&NDBCTL DATEFMT=NO

The data unload code is as follows:

&FILE ADD 20                        -* header for data portion
&NRECS = 0                          -* num records unloaded
&NDBGET &DBNAME SEQ=USEQ FORMAT=UFMT MODFLD=YES
-* read a record, return the fields modified in ZMODFLD,
-* ZVARCNT
&DOWHILE &NDBRC = 0                 -* till eof
   &NRECS = &NRECS + 1              -* 1 more record
   &FILE ADD 21 &NDBRID &ZVARCNT    -* record header
   &I = 1                           -* field index counter
   &DOWHILE &I LE &ZVARCNT          -* for all defined
                                    -* fields
      &FX = &SUBSTR &ZMODFLD 2      -* get unique field
                                    -* number
      &VARTABLE GET ID=FTAB +       -* and associated
                                    -* field name
         KEY=FX +
         FIELDS=DATA +
         VARS=FNAME
      &FILE ADD 22 &FX &FNAME &FX   -* write field
                                    -* num/name/value
      &I = &I + 1                   -* next field
      &LOOPCTL 1000                 -* prevent blowup
   &DOEND                           -* end all fields
   &FILE ADD 23 &ZVARCNT            -* end record
   &NDBGET &DBNAME SEQ=USEQ FORMAT=UFMT MODFLD=YES
                                    -* get next
      &LOOPCTL 1000                 -* prevent blowup
&DOEND                              -* end record loop
&FILE ADD 29 &NRECS                 -* num logical records

This code completes the unloading of data. All that is left is to close the files:

&NDBCLOSE &DBNAME
&FILE CLOSE &UNNAME

Other people can now use the database. The unloaded data can be processed as necessary, for example, copied to tape.

This example procedure is in the NCL distribution library as member $NDBUNLD. The code is as shown previously. See the source for more information.