标签:test 赋值 更新 -418 where ase exe exec 指示
在SQL语句中,,使用主变量 和 指示变量的方法
exec sql begin declare section;
char id[6];
smallint indic; char pub_name[41];
exec sql end declare section;
exec sql select pub_id into :id indicator :indic
from titles
where title like "%Stress%";
if (indic == -1) { printf("\npub_id is null"); }
else {
exec sql select pub_name into :pub_name
from publishers
where pub_id = :id;
printf("\nPublisher: %s", pub_name); }
程序实例: 依次检查某个系的学生记录,交互式更新某些学生年龄
EXEC SQL BEGIN DEC LARE SECTION; /*主变量说明开始*/
char deptname[64];
char HSno[64];
char HSname[64];
char HSsex[64];
int HSage;
int NEWAGE;
EXEC SQL END DECLARE SECTION; /*主变量说明结束*/
long SQLCODE;
EXEC SQL INCLUDE sqlca; /*定义SQL通信区*/
int main(void) /*C语言主程序开始*/
{ int count = 0;
char yn; /*变量yn代表yes或no*/
printf("Please choose the department name(CS/MA/IS): ");
scanf("%s", deptname); /*为主变量deptname赋值*/
EXEC SQL CONNECT TO TEST@localhost:54321 USER"SYSTEM" /"MANAGER"; /*连接数据库TEST*/
EXEC SQL DECLARE SX CURSOR FOR /*定义游标*/
SELECT Sno, Sname, Ssex, Sage /*SX对应语句的执行结果*/
FROM Student
WHERE SDept = :deptname;
EXEC SQL OPEN SX; /*打开游标SX便指向查询结果的第一行*/
for ( ; ; ) /*用循环结构逐条处理结果集中的记录*/
{ /*推进游标,将当前数据放入主变量*/
EXEC SQL FETCH SX INTO :HSno, :HSname, :HSsex,:HSage;
if (sqlca.sqlcode != 0) /* sqlcode != 0,表示操作不成功*/
break; /*利用SQLCA中的状态信息决定何时退出循环*/
if(count++ == 0) /*如果是第一行的话,先打出表头*/
printf("\n%-10s %-20s %-10s %-10s\n", "Sno", "Sname", "Ssex", "Sage");
/*打印查询结果*/
printf("%-10s %-20s %-10s %-10d\n", HSno, HSname, HSsex, HSage);
/*询问用户是否要更新该学生的年龄*/
printf("UPDATE AGE(y/n)?");
do{
scanf("%c",&yn);
} while(yn != ‘N‘ && yn != ‘n‘ && yn != ‘Y‘ && yn != ‘y‘);
if (yn == ‘y‘ || yn == ‘Y‘) /*如果选择更新操作*/
{ printf("INPUT NEW AGE:");
scanf("%d",&NEWAGE); /*用户输入新年龄到主变量中*/
EXEC SQL UPDATE Student /*嵌入式SQL*/
SET Sage = :NEWAGE
WHERE CURRENT OF SX ;
} /*对当前游标指向的学生年龄进行更新*/
}
EXEC SQL CLOSE SX; /*关闭游标SX不再和查询结果对应*/
EXEC SQL COMMIT WORK; /*提交更新*/
EXEC SQL DISCONNECT TEST; /*断开数据库连接*/
}
标签:test 赋值 更新 -418 where ase exe exec 指示
原文地址:http://www.cnblogs.com/wangprince2017/p/7677711.html