SAND CDBMS Tools Reference Guide
NDL++ Functions:
Syntax and Examples


Return to String Function Index

 

String Function Syntax and Examples


LOOKUP( )

The LOOKUP() function is used to search for a key value in either a table, the output of an SQL statement, or the fields in the RECORD or table section of a specification file. If the key value is found, LOOKUP() returns the value (or attribute) from a field in the same row as the first match. If the key value is not found, a user-specified default value is returned instead.

Syntax:

LOOKUP('lookup-name',
       'attribute-field',
      { attribute-default | NULL },
       'key-field',
        key-value[, 'key-field2',
                     key-value2[, 'key-field3',
                                   key-value3] ] )

where:

As indicated by the syntax, this function can have five, seven, or nine parameters. There can be one, two, or three key-field/key-value pairs. Each argument, except for attribute-default and the key values, is an explicit character constant, and must be quoted. The attribute-default parameter and the key values can be character expressions.

Note:
LOOKUP() will stop searching the source data as soon as it finds a match for the specified key value(s). If the source data contain multiple occurrences of matching key value(s), LOOKUP() returns a value only for the first one it encounters. There is no way to predict what order the data will be searched. If required, it is up to the user to ensure that the data set "key" is unique.


Initializing a LOOKUP Function Instance

All instances of the LOOKUP() function must be initialized in the INIT section of the specification file. The INIT section must appear after the LIBRARY section(s) and before all other sections (TYPE, VAR, RECORD, and so on). The INIT section begins with the INIT keyword and contains a list of one or more LOOKUP instance (or library) initialization statements, all between braces { }.

A LOOKUP instance declaration provides the following information: the lookup source, a list of attribute fields, and a list of key fields. Since a LOOKUP call can only use attribute and key fields that are declared in the INIT section, ensure that all fields that will be used in the script are accounted for in the initialization. At the same time, all unused attribute and key fields should be removed from the function declaration.

The function declaration has the following format:

LOOKUP 'lookup-name'
{
  [ LOOKUPBUFFER : buffer-size ]
    <source parameters>
    ATTRIBUTES   : attribute-fields-list
    KEYS         : key-fields-list
}

where:

<source parameters>

There are two optional SCRIPT modes: COLUMN and TABLE.

  • In COLUMN mode (the default), the LOOKUP() function uses the data described in the RECORD section as the lookup source.
  • In TABLE mode, the LOOKUP() function uses the data described in the map specification section as the lookup source.

In addition, some of the ndlm command line flags may be included with the SCRIPT parameter, to allow the user extra control over how the data is to be processed. The available ndlm flags are the following (consult the Running ndlm section for further details):

-b
-e file-name
-f buffer-size
-i bytes
-l
-n null-string
-o {0 | 1}
-q
-s rows
-z {0 | 1}

If a flag not in the above list is included, or a flag value is illegal, processing of the LOOKUP() function will not be halted, but the rest of the flag string will be ignored.

All flags and their values must have surrounding quotation marks, for example:

'-f 1024 -b -o 1'


Example (INIT section)

INIT
{

LOOKUP 'L1'
{
    SQL        : SELECT * FROM T;
    ATTRIBUTES : 'A1', 'A2', 'A3'
    KEYS       : 'K1', 'K2'
}

LOOKUP 'L2'
{
    LOOKUPBUFFER : 64
    TABLE        : 'T'
    ATTRIBUTES   : 'A1', 'A2', 'A3'
    KEYS         : 'K1', 'K2'
}

LOOKUP 'L3'
{
    LOOKUPBUFFER : 2048
    SCRIPT       : @scriptfile1 COLUMN '-f 1024 -b -o 0'
    ATTRIBUTES   : 'A1', 'A2', 'A3'
    KEYS         : 'K1', 'K2'
}

LOOKUP 'L4'
{
    SCRIPT     : @scriptfile1 TABLE '-f 1024 -b -o 1'
    ATTRIBUTES : 'A1', 'A2', 'A3'
    KEYS       : 'K1', 'K2'
}

}


Example (SELECT statement)

IMPORT @load.dat
{
    INIT
    {
        LOOKUP 'SColors'
        {
            LOOKUPBUFFER : 1024
            SQL          : SELECT C.ColorName, C.ColorIndex, C.ColorPrice
                           FROM Colors C, Inventory I
                           WHERE C.ColorIndex = I.ColorIndex
                           AND I.Qty > 0 ;
            ATTRIBUTES   : 'ColorIndex', 'ColorPrice'
            KEYS         : 'ColorName'
        }
    }
    RECORD
    {
        field1 *\r\n
    }
    colortable
    {
        ColorIndex VALI(LOOKUP('SColors', 'ColorIndex', '-1',
                               'ColorName', field1) )
        ColorPrice VALF(LOOKUP('SColors', 'ColorPrice', '0',
                               'ColorName', field1) )
    }
}

In this sample script, a LOOKUP instance with the name 'SColors' is declared in the INIT section, with a buffer size set to 1024 MB (1 GB). The instance uses a SELECT statement, executed against the current database, as its source. Two of the SELECT columns, 'ColorIndex' and 'ColorPrice', are listed as potential attribute fields, meaning that any LOOKUP() function in the script that references the 'SColors' instance must return an attribute from one of those two columns. Only the column 'ColorName' is listed as a key field, which means that all occurrences of the LOOKUP() function that use the 'Colors' instance must search for the key value in the 'ColorName' column only.

In the example, the first LOOKUP call returns an attribute from the ‘ColorIndex’ column if the key value (field1) is found in the 'ColorName' column; otherwise it returns '-1'. The second LOOKUP call returns an attribute from the 'ColorPrice' column if the key value (field1) is found in the 'ColorName' column; otherwise it returns '0'.


Example (TABLE)

IMPORT @load.dat
{
    INIT
    {
        LOOKUP 'TColors'
        {
            LOOKUPBUFFER : 1024
            TABLE : 'Colors'
            ATTRIBUTES : 'ColorIndex', 'ColorPrice'
            KEYS : 'ColorName'
        }
    }
    RECORD
    {
        field1 *\r\n
    }
    colortable
    {
        ColorIndex VALI(LOOKUP('TColors', 'ColorIndex', '-1',
                               'ColorName', field1) )
        ColorPrice VALF(LOOKUP('TColors', 'ColorPrice', '0',
                               'ColorName', field1) )
    }
}

In this script, which is a variation of the SELECT example, a LOOKUP instance with the name 'TColors' is declared in the INIT section. Instead of specifying a SELECT statement as its lookup source, this script specifies a single table (Colors). A SELECT statement is automatically generated and applied against the database, using the fields listed under Attributes and Keys as projections from the specified table. In this case, the SQL would be the following:

SELECT ColorIndex, ColorPrice, ColorName
FROM Colors;

Table mode should be used only when the lookup source is a single table, and the intended SQL is a simple, unfiltered SELECT from that table.

 

Return to String Function Index