The HASH_STORE function adds and updates rows in a Hash Table. Hash Tables allow data to be stored in-memory for fast, efficient retrieval during processing. They are most commonly used for lookups to reference data rather than selecting data from a table in relational database.
The performance improvement that Hash Tables provide vs a physical relational tables is significant, with higher speed and much lower CPU consumption. Hash Tables consist of a key and user data, where the user data is stored as a contiguous set of fixed length columns. When a row is retrieved from a hash table, the user data is returned in the same contiguous format in which it was stored. For example, if you added a row to a hash table with the key and user data for 3 columns, the data is stored like this in the table.
key|colum1column2column3
It is the responsibility of the programmer to understand the structure/position of the user data and reference it accordingly. Before data can be stored in a hash table, the hash object must be Declared in the Field Specification section of the script. In this example, we will create a hash table named H_EMPTBL1.
DECLARE H_EMPTBL1 AS HASH;
If the hash table is used only for validation and it is only necessary to know whether a row exists in the table, it may not be necessary to define anything else. However, if you are planning on storing more than Key data in the hash table, it will also be necessary to DECLARE a variable large enough to hold the data returned. Let us assume that you need to store 49 bytes of data for each Key Value in the hash table. That will require a variable of 49 characters, e.g:
DECLARE V_EMPDATA 49 ' ';
Once the hash table has been defined and data stored in the table, rows can be retrieved and removed using the HASH_LOAD and HASH_REMOVE functions.
Category
Specialized
HASH_STORE(hash_table, hash_key,hash_data)
Parameter | Description |
---|---|
hash_table | The name of a previously declared hash_table. |
hash_key | This parameter specifies the value that will be compared against the key of the hash_table. The search value may be a field/column of a source datastore, a literal value (i.e. ABC), a variable or the result of another Function. |
hash_data | This parameter specifies the data that will be added/updated to a row of the hash_table. The single parameter may represent any number contiguously stored fixed length columns. It the developers responsibility to properly concatenate the data to be stored. |
Example
HASH_STORE(H_EMPTBL1, EMPNUM, STRING(SSN, LNAME, DEPT))
HASH_STORE(H_EMPTBL1, STRING(EMPNUM,LNAME), STRING(SSN, LNAME, DEPT))