/****************************************************************************** ** ** Source File Name = updat.sqc ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE: This sample program demonstrates the use of static SQL. ** It updates all managers in the STAFF table of the ** SAMPLE database and changes their job from 'Mgr' to ** 'Clerk', deletes all who are 'Sales', and inserts a new ** row. In all three SQL statements (UPDATE, DELETE, INSERT) ** a host variable is implemented. ** Finally, a ROLLBACK is done so that the SAMPLE ** database remains unchanged. ** ** EXTERNAL DEPENDENCIES : ** - Ensure existence of database for precompile purposes. ** - Precompile with the SQL precompiler (PREP in DB2) ** - Bind to a database (BIND in DB2) ** - Compile and link with the IBM Cset++ compiler (AIX and OS/2) ** or the Microsoft Visual C++ compiler (Windows) ** or the compiler supported on your platform. ** ** For more information about these samples see the README file. ** ** For more information on programming in C, see the: ** - "Programming in C and C++" section of the Application Development Guide ** For more information on Building C Applications, see the: ** - "Building C Applications" section of the Application Building Guide. ** ** For more information on the SQL language see the SQL Reference. ** *******************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sqlenv.h> #include "utilemb.h" EXEC SQL INCLUDE SQLCA; /* :rk.1:erk. */ int main(int argc, char *argv[]) { EXEC SQL BEGIN DECLARE SECTION; /* :rk.2:erk. */ char statement[256]; char userid[9]; char passwd[19]; char jobUpdate[6]; EXEC SQL END DECLARE SECTION; printf( "\nSample C program: UPDAT \n"); if (argc == 1) { EXEC SQL CONNECT TO sample; EMB_SQL_CHECK("CONNECT TO SAMPLE"); } else if (argc == 3) { strcpy (userid, argv[1]); strcpy (passwd, argv[2]); EXEC SQL CONNECT TO sample USER :userid USING :passwd; /* :rk.3:erk. */ EMB_SQL_CHECK("CONNECT TO SAMPLE"); } else { printf ("\nUSAGE: updat [userid passwd]\n\n"); return 1; } /* endif */ strcpy (jobUpdate, "Clerk"); EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = 'Mgr'; /* :rk.4:erk. */ EMB_SQL_CHECK("UPDATE STAFF"); printf ("All 'Mgr' have been demoted to 'Clerk'!\n" ); strcpy (jobUpdate, "Sales"); EXEC SQL DELETE FROM staff WHERE job = :jobUpdate; /* :rk.5:erk. */ EMB_SQL_CHECK("DELETE FROM STAFF"); printf ("All 'Sales' people have been deleted!\n"); EXEC SQL INSERT INTO staff VALUES (999, 'Testing', 99, :jobUpdate, 0, 0, 0); /* :rk.6:erk. */ EMB_SQL_CHECK("INSERT INTO STAFF"); printf ("New data has been inserted\n"); EXEC SQL ROLLBACK; /* :rk.7:erk. */ EMB_SQL_CHECK("ROLLBACK"); printf( "On second thought -- changes rolled back.\n" ); EXEC SQL CONNECT RESET; EMB_SQL_CHECK("CONNECT RESET"); return 0; } /* end of program : updat.sqc */