2. Embedded SQL for C : C Variables and Data Types : Variable Usage : Varchar Variables for Logical Key Data Types
 
Share this page                  
Varchar Variables for Logical Key Data Types
It is recommended that you use varchar variables to retrieve or insert Ingres logical key data types instead of char(8) or char(16) compatible variables. If logical key data contain embedded nulls, the Ingres runtime system may not be able to detect the end-of-string terminator on char variables; using varchar will eliminate this confusion between null-terminated strings and null data. System maintained logical keys are very likely to contain binary data including null bytes; therefore, you should always use a varchar variable when dealing with system maintained logical keys.
Example: Varchar variable usage
exec sql begin declare section;

exec sql declare keytab table
     (tkey table_key with system_maintained,
     okey object_key with sytem_maintained,
     row integer);

exec sql declare savetab table
     (tsave table_key not system_maintained,
     osave object_key not system_maintained);

#define tablen 8 /* Table_key length */
#define objlen 16 /* Object_key length */
varchar struct
{
    short obj_len;
    char obj_data[OBJLEN]];
 } objvar;

varchar struct
{
    short tab_len;
    char tab_data[TABLEN];
 } tabvar;

int indx;
 short tabind, objind;

exec sql end declare section;
  . .

exec sql insert into keytab (row) values (1);
/*
** Retrieve the table key and object key values
** that were just inserted by the system. Then
** INSERT the table key and object key values into
** another table with non-system maintained logical keys.
*/
exec sql inquire_sql (:tabvar:tabind = table_key,
                        :objvar:objind= object_key);
 if (tabind == -1 || objind == -1)
      printf ("No logical key values available.\n");
else
     exec sql insert into savetab (tsave, osave)
     values (table_key(:tabvar), object_key(:objvar));

/*
** Select data from a table that contains logical key
** data types.
*/
exec sql select tsave, osave into :tabvar, :objvar
      from savetab;
exec sql begin;
      /*Print out the table key value in Hex */
      printf (" Table key value = 0x");
           for (indx = 0; indx < tabvar.tab_len; indx++)
     {
                 printf ("%02x", (unsigned char)
    tabvar.tab_data[indx]);
         }
         printf ("\n");
exec sql end;