代码:xx.pc
/*
功能:演示了Oracle使用指示变量(专门用来处理NULL值)
指示变量专门用于处理数据库的NULL值,它是一种short类型的C语言变量。
当执行内嵌SELECT语句或FETCH语句时,如果不引用指示变量,并且返回值为NULL,
那么会显示如下错误信息:ORA-01405: 读取的列值为NULL
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlca.h>
#pragma comment(lib, "orasql10.lib")
int connect();
void sql();
void sql_error();
void main()
{
EXEC SQL WHENEVER SQLERROR DO sql_error(); // 安装错误处理句柄
if(connect() == 0)
{
sql();
EXEC SQL COMMIT RELEASE; // 提交事务,断开连接
}
else
printf("连接失败\n");
}
int connect() // connect to oracle database
{
char username[10], password[10], server[10];
strcpy(username, "scott");
strcpy(password, "scott");
strcpy(server, "orcl");
EXEC SQL CONNECT :username IDENTIFIED BY :password USING :server;
if(sqlca.sqlcode == 0)
return 0;
else
return sqlca.sqlcode;
}
void sql_error() // print error infomation
{
printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
}
void sql()
{
int no; // 定义宿主变量
char name[10];
float commission;
short comm_ind; // 定义指示变量
while(1)
{
printf("请输入雇员号(0: 退出):");
scanf("%d", &no);
if(no == 0)
break;
EXEC SQL Select ename, comm into :name, :commission:comm_ind from emp where empno=:no;
if(sqlca.sqlcode == 0)
{
if(comm_ind == 0)
printf("雇员名:%s, 补助: %.2f\n", name, commission);
else
printf("雇员名:%s, 补助为NULL\n", name);
}
else
printf("该雇员不存在\n");
}
printf("sqlca.sqlerrd[2] = %d\n", sqlca.sqlerrd[2]); // sqlca.sqlerrd[2]存放着Select语句作用的行数
}