#include #include #include EXEC SQL INCLUDE SQLCA; #define CHECKERR(CE_STR) if (sqlca.sqlcode != 0) {printf("%s failed. Reason %ld\n", CE_STR, sqlca.sqlcode); exit(1); } int main(int argc, char *argv[]) { EXEC SQL BEGIN DECLARE SECTION; char pname[10]; short dept; char userid[9]; char passwd[19]; char st[255]; char parm_var[6]; EXEC SQL END DECLARE SECTION; printf( "Sample C program: VARINP \n" ); if (argc == 1) { EXEC SQL CONNECT TO sample; CHECKERR ("CONNECT TO SAMPLE"); } else if (argc == 3) { strcpy (userid, argv[1]); strcpy (passwd, argv[2]); EXEC SQL CONNECT TO sample USER :userid USING :passwd; CHECKERR ("CONNECT TO SAMPLE"); } else { printf ("\nUSAGE: varinp [userid passwd]\n\n"); return 1; } /* endif */ strcpy (st, "SELECT name, dept FROM staff WHERE job = ? FOR UPDATE OF job"); EXEC SQL PREPARE s1 FROM :st; (1) CHECKERR ("PREPARE"); EXEC SQL DECLARE c1 CURSOR FOR s1; (2) strcpy (parm_var, "Mgr"); EXEC SQL OPEN c1 USING :parm_var; (3) CHECKERR ("OPEN"); strcpy (parm_var, "Clerk"); strcpy (st, "UPDATE staff SET job = ? WHERE CURRENT OF c1"); EXEC SQL PREPARE s2 from :st; (4) do { EXEC SQL FETCH c1 INTO :pname, :dept; (5) if (SQLCODE != 0) break; printf( "%-10.10s in dept. %2d will be demoted to Clerk\n", pname, dept ); EXEC SQL EXECUTE s2 USING :parm_var; (6) CHECKERR ("EXECUTE"); } while ( 1 ); EXEC SQL CLOSE c1; (7) CHECKERR ("CLOSE CURSOR"); EXEC SQL ROLLBACK; CHECKERR ("ROLLBACK"); printf( "\nOn second thought -- changes rolled back.\n" ); EXEC SQL CONNECT RESET; CHECKERR ("CONNECT RESET"); return 0; } /* end of program : VARINP.SQC */ 1.Prepare the SELECT SQL statement The PREPARE statement is called to dynamically prepare an SQL statement. In this SQL statement, parameter markers are denoted by the ?. The job field of staff is defined to be updatable, even though it is not specified in the result table. 2.Declare the cursor. The DECLARE CURSOR statement associates the cursor c1 to the query that was prepared in (1). 3.Open the cursor. The cursor c1 is opened, causing the database manager to perform the query and build a result table. The cursor is positioned before the first row. 4.Prepare the UPDATE SQL statement The PREPARE statement is called to dynamically prepare an SQL statement. The parameter marker in this statement is set to be Clerk but can be changed dynamically to anything, as long as it conforms to the column data type it is being updated into. 5.Retrieve a row. The FETCH statement positions the cursor at the next row and moves the contents of the row into the host variables. This row becomes the CURRENT row. 6.Update the current row. The current row and specified column, job, is updated with the content of the passed parameter parm_var. 7.Close the cursor. The CLOSE statement is issued, releasing the resources associated with the cursor. The cursor can be opened again, however. The CHECKERR macro/function is an error checking utility which is external to the program. The location of this error checking utility depends upon the programming language used: C check_error is redefined as CHECKERR and is located in the util.c file.