Fetch a row from a table

In this example, the table is not updated and is not password protected, so an auto-open will be used (no open command will be issued). At the end of the job, the table will not be closed but will be released by the operating system.

In COBOL
 PROCEDURE DIVISION.
***  FETCH A TABLE ROW USING ITS KEY.
     MOVE 'FK'                   TO xxxx-COMMAND.
     CALL 'TBLBASE' USING        TB-PARM
                                 xxxx-COMMAND-AREA
                                 xxxx-ROW-AREA
                                 DATA-KEY.
     IF  xxxx-FOUND = 'Y'
         PERFORM FOUND-ROUTINE
     ELSE
         PERFORM NOT-FOUND-ROUTINE.
In C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dkh.h"

/*
 * DK1TEX0C
 *
 * Fetch a row from a table
 *
 * In this example, we will fetch one row from
 * the table with the key supplied by the user.
 * Auto-open will be used and the table will be
 * released upon program termination.
 *
 * Please refer to other document for the
 * initXXXX and fixStringLength functions.
 */
 
/*
 * Assume these are user inputs.
 */
static char szTableName[6] = "AARON";
static char szKey[6] = "TIGER";
static char szStatus[6] = "NYYYN";
static int nGen = 0;

int main(void)
{
    TbParmStruct tbParm;
    TbCommandAreaStruct tbCommArea;
    TbTableDefinitionStruct tbTableDef;
    char * pRowArea = NULL;
    char * pKeyArea = NULL;
    char sStatus[8];
    char sTableName[8];
    int nGeneration = nGen;
 
    /*
     * Initialize the parameters.
     */
    fixStringLength( szTableName, sTableName, 8 );
    InitTbParm( &tbParm );
    InitTbCommandArea( &tbCommArea, sTableName );
    InitTableDef( &tbTableDef );

    /*
     * Initialize tableBASE with CS, ChangeStatus.
     */
    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;
    }
    
    /*
     * GD, get table definition to retrieve the row length and
     * key length.
     */
    memcpy( tbCommArea.tbCommand, "GD", 2 );
    TBLBASE( &tbParm, &tbCommArea, &tbTableDef, nGeneration );
    if( (tbCommArea.tbError != TB_SUCCESS) || tbCommArea.tbFound == 'N' )
    {
        printf( "GD\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;
    }
    
    /*
     * Allocate spaces for a row (with an additional string
     * terminator) and a key.
     */
    pRowArea = (char *) malloc( tbTableDef.rowSize + 1 );
    if( pRowArea == NULL )
        return TB_ERROR;
    memset( pRowArea, ' ', tbTableDef.rowSize);
    pKeyArea = (char *) malloc( tbTableDef.keySize );
    if( pKeyArea == NULL )
        return TB_ERROR;
    memset( pKeyArea, ' ', tbTableDef.keySize);
    
    /*
     * Call tableBASE with FK, FetchbyKey.
     */
    fixStringLength( szKey, pKeyArea, tbTableDef.keySize );
    memcpy( tbCommArea.tbCommand, "FK", 2 );
    TBLBASE( &tbParm, &tbCommArea, pRowArea, pKeyArea );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "FK\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        free( pRowArea );
        free( pKeyArea );
        return tbCommArea.tbError;
    }
    else
    {
        /*
         * If the key is not found, it will print a row with spaces.
         * You can check the found code in CommandArea to make sure
         * the row was found.
         */
        pRowArea[tbTableDef.rowSize] = '\0';
        printf( "FK: %s\n", pRowArea );
    }
    if( pRowArea != NULL )
        free( pRowArea );
    if( pKeyArea != NULL )
        free( pKeyArea );
    return TB_SUCCESS;
}
In PL/I
%process s list map compile xref attributes limits(extname(30));
fetchpli: proc options(main);
/******************************************************************/
/* This program demonstrates how to fetch a table row using a key */
/* without opening the table; an implicit or auto-open is used    */
/******************************************************************/
declare
  tblbase external entry options(asm);
/*                                                                */
/* tbparmsp contains the PL/I copybooks for TBPARM and TB-COMMAND */
/* and can be found in your distribution library                  */
/*                                                                */
%include tbparmsp;
/*                                                                */
/* row area for table01                                           */
/*                                                                */
dcl tb_row_area char(99); 
dcl tb_key_area char(34);
/*                                                                */
/* fetch a row from the table                                     */
/*                                                                */
    tb_command_code = 'FK';
    tb_table = 'EXAMPLE';
    tb_key_area = 'BROWN               GEORGE              ';
    call tblbase(tb_parm,
                 tb_command,
                 tb_row_area,
                 tb_key_area);
    put list (tb_row_area) skip;
    put list ('or return code:',tb_error) skip;
end fetchpli;