Access a table that may not exist

In this example, the name of a table is obtained from an input source. The program must open the table for write, from a library, or if that fails, the program must create a new table. To prevent an abend when opening a non-existent table, the abend status will be temporarily overridden to bypass abend processing. As soon as the open command has been issued, the status will automatically be reset to permit abends. If the table cannot be found on the library (error code 0009), a new table will be defined. If any other error occurs, repeating the open command will cause tableBASE to abend.

In COBOL
*** (table name is obtained from the data)
     MOVE TABLE-NAME                TO xxxx-TABLE
*** FOR THE NEXT COMMAND TURN OFF TABLEBASE ABEND FUNCTION
     MOVE 'Y'                       TO xxxx-ABEND-OVERRIDE

*** TRY TO FIND TABLE
     MOVE 'OW'                      TO xxxx-COMMAND
     CALL 'TBLBASE'           USING TB-PARM
                                    xxxx-COMMAND-AREA

     IF xxxx-ERROR-CODE NOT = 0
         IF xxxx-ERROR-CODE = 9
*** TABLE NOT FOUND, DEFINE A NEW TABLE
            MOVE 'DT'               TO xxxx-COMMAND
            CALL 'TBLBASE'    USING TB-PARM
                                    xxxx-COMMAND-AREA
                                    xxxx-DEFINE-BLOCK
         ELSE
***  FOR ANY OTHER ERROR, CAUSE TABLEBASE TO ABEND
            MOVE 'OW'               TO xxxx-COMMAND
            CALL 'TBLBASE'    USING TB-PARM
                                    xxxx-COMMAND-AREA
         END-IF
     END-IF

*** (continue processing with open table)
In C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dkh.h"

/*
 * DK1TEX5C
 *
 * Access a table that may or may not exist.
 */
 
/*
 * Assume these are user inputs.
 */
static char szTableName[7] = "AARON2";
static char szStatus[6] = "NYYYN";
static char szStatusAbend[6] = "YYYYN";
static char szWritePassword[2] = " ";
static int nGen = 0;
static int nRowSize = 20;
static int nKeySize = 5;
static int nKeyLocaiton = 1;

int main(void)
{
    TbParmStruct tbParm;
    TbCommandAreaStruct tbCommArea;
    TbTableDefinitionStruct tbTableDef;
    char sWritePassword[8];
    char sStatus[8];
    char sTableName[8];
    int nGeneration = nGen;
    
    /*
     * Initialize the parameters.
     */
    fixStringLength( szTableName, sTableName, 8 );
    InitTbParm( &tbParm );
    InitTbCommandArea( &tbCommArea, sTableName );
    
    /*
     * Initialize tableBASE with CS, ChangeStatus.
     * As usual, this disable the abend.
     */
    fixStringLength( szStatus, sStatus, 8 );
    memcpy( tbCommArea.tbCommand, "CS", 2 );
    TBLBASE( &tbParm, &tbCommArea, sStatus );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "CS\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    /*
     * Call tableBASE with OW, OpenforWrite.
     */
    fixStringLength( szWritePassword, sWritePassword, 8 );
    memcpy( tbCommArea.tbCommand, "OW", 2 );
    TBLBASE( &tbParm, &tbCommArea, sWritePassword, nGeneration);
    if( tbCommArea.tbError == 9 )
    {
        /*
         * Create a new table with DT, DefineTable.
         */
        memcpy( tbCommArea.tbCommand, "DT", 2 );
        InitTableDef( &tbTableDef );
        memcpy( tbTableDef.writePassword, sWritePassword, 8 );
        tbTableDef.org = 'H';
        tbTableDef.method = 'H';
        tbTableDef.rowSize = nRowSize;
        tbTableDef.keySize = nKeySize;
        tbTableDef.keyLocation = nKeyLocaiton;
        TBLBASE( &tbParm, &tbCommArea, &tbTableDef );
        if( tbCommArea.tbError != TB_SUCCESS )
        {
            printf( "DT\n");
            printf( "Found code: %c\n", tbCommArea.tbFound );
            printf( "Error code: %d\n", tbCommArea.tbError );
            printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
            return tbCommArea.tbError;
        }
        else
        {
            printf( "A new table was created in memory.\n" );
        }
    }
    if( (tbCommArea.tbError != TB_SUCCESS)
          && (tbCommArea.tbError != 9) )
    {
        /*
         * Any error from OW, will cause tableBASE to abend
         * (abort) if the abend switch is turned on.
         */
        /*
         * Setup tableBASE to enable abend on errors (the first
         * byte of the status is set to 'Y' instead of 'N').
         * A simpler way to do that would have been to set
         * tbCommArea.tbAbendOverride to 'Y' before calling
         * Open for Write (setting the tbAbendOverride field only
         * works once, it is automatically reset to ' ' by TBLBASE()).
         */
        fixStringLength( szStatusAbend, sStatus, 8 );
        memcpy( tbCommArea.tbCommand, "CS", 2 );
        TBLBASE( &tbParm, &tbCommArea, sStatus );
        if( tbCommArea.tbError != TB_SUCCESS )
        {
            printf( "CS\n");
            printf( "Found code: %c\n", tbCommArea.tbFound );
            printf( "Error code: %d\n", tbCommArea.tbError );
            printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
            return tbCommArea.tbError;
        }
        
        fixStringLength( szWritePassword, sWritePassword, 8 );
        memcpy( tbCommArea.tbCommand, "OW", 2 );
        TBLBASE( &tbParm, &tbCommArea, sWritePassword, nGeneration);
        /*
         * We should never reach this point.
         */
    }
    
    /*
     * You can do whatever you want with the newly defined table
     * or the table just opened. However, you should remember to
     * store and close the table before exiting the program,
     * otherwise all the changes to the table will be lost.
     */
    printf( "The table will be closed and released upon termination.\n" );

    return TB_SUCCESS;
}