完全版 ESQL/C资料二( 三 )


#include
#include
#include
#include "util.h"
EXEC SQL INCLUDE SQLCA;
#define CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0) return 1;
int main(int argc, char *argv[]) {
EXEC SQL BEGIN DECLARE SECTION;
char pname[10];
short dept;
char userid[9];
char passwd[19];
EXEC SQL END DECLARE SECTION;
printf( "Sample C program: CURSOR 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: cursor [userid passwd]nn");
return 1;
} /* endif */

EXEC SQL DECLARE c1 CURSOR FOR (1)
SELECT name, dept FROM staff WHERE job="Mgr"
FOR UPDATE OF job;
EXEC SQL OPEN c1; (2)
CHECKERR ("OPEN CURSOR");
do {
EXEC SQL FETCH c1 INTO :pname, :dept; (3)
if (SQLCODE != 0) break;
printf( "%-10.10s in dept. - will be demoted to Clerkn",
pname, dept );
} while ( 1 );
EXEC SQL CLOSE c1; (4)
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 : CURSOR.SQC */
在上面这个程序中,
(1)定义了一个游标,并指明游标的名字为C1,同时给出了相对于游标的查询语句和游标类型(UPDATE) 。
(2)打开游标 。系统执行查询语句,建立结果表,将游标指针指向第一条记录之前 。
(3)FETCH语句将指针的下一条记录取出,将记录中的数据存放在相应的宿主变量中 。同时指针下移 。
(4)用CLOSE关闭游标 。3.2.4插入、删除和修改操作
DB2中的插入、删除和修改操作同SQL语句中INSERT、DELETE和UPDATE语句类似 。只需在相应的SQL语句前加上EXEC SQL即可 。请看下面这个例子:
例、将staff表中所有工作为“Mgr”的职工的工作改变为“clerk”,并将staff表中所有工作为“sale”的职工信息删除 。最后插入一新行 。
#include
#include
#include
#include
#include "util.h"
EXEC SQL INCLUDE SQLCA; (1)
#define CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0) return 1;
int main(int argc, char *argv[]) {
EXEC SQL BEGIN DECLARE SECTION; (2)
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;
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; (3)
CHECKERR ("CONNECT TO SAMPLE");
}
else {
printf ("nUSAGE: updat [userid passwd]nn");
return 1;
} /* endif */
strcpy (jobUpdate, "Clerk");
EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = "Mgr"; (4)
CHECKERR ("UPDATE STAFF");
printf ("All "Mgr" have been demoted to "Clerk"!n" );
strcpy (jobUpdate, "Sales");
EXEC SQL DELETE FROM staff WHERE job = :jobUpdate; (5)
CHECKERR ("DELETE FROM STAFF");
printf ("All "Sales" people have been deleted!n");
EXEC SQL INSERT INTO staff
VALUES (999, "Testing", 99, :jobUpdate, 0, 0, 0); (6)
CHECKERR ("INSERT INTO STAFF");
printf ("New data has been insertedn");
EXEC SQL ROLLBACK; (7)
CHECKERR ("ROLLBACK");
printf( "On second thought -- changes rolled back.n" );
EXEC SQL CONNECT RESET;
CHECKERR ("CONNECT RESET");
return 0;
}
/* end of program : UPDAT.SQC */
上述语句:
(1)包含SQLCA结构 。该结构用于将SQL语句执行的结果信息返回给应用程序 。
(2)宿主变量定义 。
(3)连接到DB2的SAMPLE数据库 。
(4)UPDATE语句将staff表中所有工作为“Mgr”的职工的工作改变为“clerk” 。
(5)DELETE语句将staff表中所有工作为“sale”的职工信息删除 。

推荐阅读