Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Import libraries [Table of Contents] Structure of Embedded SQL programs

[-] Chapter 2: The Embedded SQL Interface
[-] Application development using Embedded SQL
[-] A simple example


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 );
 return( -1 );
}

This example connects to the database, updates the surname of employee number 1056, 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.

Note that the first section of this chapter uses UPDATE and INSERT examples because they are simpler.

For a description of fetching data, see Fetching data .


Import libraries [Table of Contents] Structure of Embedded SQL programs