Contents Index Import libraries Structure of embedded SQL programs

ASA Programming Guide
  Embedded SQL Programming
    Introduction

A simple example


The following is a very simple example of an embedded SQL program.

#include <stdio.h>
EXEC SQL INCLUDE SQLCA;
main()
{
   db_init( &sqlca );
   EXEC SQL WHENEVER SQLERROR GOTO error;
   EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";
   EXEC SQL UPDATE employee
      SET emp_lname =    'Plankton'
      WHERE emp_id = 195;
   EXEC SQL COMMIT WORK;
   EXEC SQL DISCONNECT;
   db_fini( &sqlca );
   return( 0 );
   
   error:
   printf( "update unsuccessful -- sqlcode = %ld.n",
      sqlca.sqlcode );
   db_fini( &sqlca );
   return( -1 );
}

This example connects to the database, updates the last name of employee number 195, commits the change, and exits. There is virtually no interaction between the SQL and C code. The only thing the C code is used for in this example is control flow. The WHENEVER statement is used for error checking. The error action (GOTO in this example) is executed after any SQL statement that causes an error.

For a description of fetching data, see Fetching data.


Contents Index Import libraries Structure of embedded SQL programs