15.10 - Include SQLDA Example - Preprocessor2 for Embedded SQL

Teradata Preprocessor2 for Embedded SQL Programmer Guide

prodname
Preprocessor2 for Embedded SQL
vrm_release
15.10
category
Programming Reference
featnum
B035-2446-035K
EXEC SQL BEGIN DECLARE SECTION;
static char  LOGON_STRING[13];
static long  EMPNUM
static long  MANNUM;
static long  DPTNUM;
static long  JOBNUM;
static short EMPIND;
static short MANIND;
static short DPTIND;
static short JOBIND;
static VARCHAR  SQL_STATEMENT[180];
static int ERROR_CODE;
static VARCHAR ERROR_MSG[81];
static short MAX_LENGTH = 80;
EXEC SQL END DECLARE SECTION;
static char  REQUEST_TYPE[9];
EXEC SQL INCLUDE SQLDA;
static struct sqlda *SQLDA;
EXEC SQL INCLUDE SqlCA;
EXEC SQL
   DECLARE EMPCUR CURSOR FOR EMPSTMT;
/*******************************************************/
/********** ERROR CHECK                                */
/*******************************************************/
static void ERROR_CHECK ()
{
if (SqlCA.SqlCode != 0)
   {
   PPRTEXT (&SQL_RDTRTCON, &ERROR_CODE, &ERROR_MSG,
   &MAX_LENGTH);
   ERROR_MSG.arr[ERROR_MSG.len] = ’\0’;
   printf (“\n”);
   printf (“ERROR/WARNING DETECTED IN %s\n”,
   REQUEST_TYPE);
   printf (“   CODE: %d\n”, ERROR_CODE);
   printf (“   MSG : %s\n”, ERROR_MSG.arr);
   }
}
/*******************************************************/
/********** FETCH                                      */
/*******************************************************/
static void FETCH_EMPCUR ()
{
strcpy (REQUEST_TYPE, “FETCH”);
EXEC SQL
   FETCH EMPCUR USING DESCRIPTOR *SQLDA;
ERROR_CHECK ();
if (SqlCA.SqlCode == 0)
   {
   printf (“\n”);
   printf (“EMPLOYEE NUMBER   : %ld\n”, EMPNUM);
   printf (“MANAGER NUMBER    : %ld\n”, MANNUM);
   printf (“DEPARTMENT NUMBER : %ld\n”, DPTNUM);
   printf (“JOB CODE          : %ld\n”, JOBNUM);
   }
}
/*******************************************************/
/********** MAIN                                       */
/*******************************************************/
main ()
{
printf (“EXECUTING DYNAMIC SAMPLE...\n\n”);
/*******************************************************/
/********** LOGON                                      */
/*******************************************************/
strcpy (REQUEST_TYPE, “LOGON”);
strcpy (LOGON_STRING, “TDP/USER,PWD”);
EXEC SQL
   LOGON :LOGON_STRING;
ERROR_CHECK ();
if (SqlCA.SqlCode != 0)
   return (1);
/*******************************************************/
/********** PREPARE                                    */
/*******************************************************/
strcpy (REQUEST_TYPE, “PREPARE”);
strcpy (SQL_STATEMENT.arr, “SELECT EMPLOYEE_NUMBER, \
MANAGER_EMPLOYEE_NUMBER, DEPARTMENT_NUMBER, JOB_CODE \
FROM EMPLOYEE ORDER BY EMPLOYEE_NUMBER“);
SQL_STATEMENT.len = strlen (SQL_STATEMENT.arr);
EXEC SQL
   DECLARE EMPSTMT STATEMENT;
   ERROR_CHECK ();
EXEC SQL
   PREPARE EMPSTMT FROM :SQL_STATEMENT;
ERROR_CHECK ();
/*******************************************************/
/********** DESCRIBE                                   */
/*******************************************************/
strcpy (REQUEST_TYPE, “DESCRIBE”);
SQLDA = (struct sqlda *) malloc (SQLDASIZE(4));
if (SQLDA == NULL)
   {
   fprintf (stderr, “Error: could not allocate SQLDA!\n”);
   return (1);
   }
memcpy (SQLDA->sqldaid, “SQLDA    ”, 8);
SQLDA->sqldabc = 0;
SQLDA->sqln    = 4;
EXEC SQL
   DESCRIBE EMPSTMT INTO *SQLDA;
ERROR_CHECK ();
if (SqlCA.SqlCode == 0)
   {
   SQLDA->sqlvar[0].sqldata = (unsigned char *) (&EMPNUM);
   SQLDA->sqlvar[0].sqlind  = &EMPIND;
   SQLDA->sqlvar[1].sqldata = (unsigned char *) (&MANNUM);
   SQLDA->sqlvar[1].sqlind  = &MANIND;
   SQLDA->sqlvar[2].sqldata = (unsigned char *) (&DPTNUM);
   SQLDA->sqlvar[2].sqlind  = &DPTIND;
   SQLDA->sqlvar[3].sqldata = (unsigned char *) (&JOBNUM);
   SQLDA->sqlvar[3].sqlind  = &JOBIND;
   }
/*******************************************************/
/********** OPEN                                       */
/*******************************************************/
strcpy (REQUEST_TYPE, “OPEN”);
EXEC SQL
   OPEN EMPCUR;
ERROR_CHECK ();
while (SqlCA.SqlCode == 0)
   FETCH_EMPCUR ();
/*******************************************************/
/********** CLOSE                                      */
/*******************************************************/
strcpy (REQUEST_TYPE, “CLOSE”);
EXEC SQL
   CLOSE EMPCUR;
ERROR_CHECK ();
/*******************************************************/
/********** LOGOFF                                     */
/*******************************************************/
strcpy (REQUEST_TYPE, “LOGOFF”);
EXEC SQL
   LOGOFF;
ERROR_CHECK ();
return (0);
}