在Pro*C中,VARCHAR类型宿主变量,如果用到嵌入式PL/SQL中,那么必须首先初始化其长度,
否则会报错“ORA-01458: 内部变量字符串长度无效” 的错误,而在嵌入式的SQL中,不必这么做。
看如下一个简单例子:
/home/cpicsrv/yansp > cat test.pc
#include
#include
exec sql include sqlca;
main()
{
exec sql begin declare section;
char *username="yansp";
char *password="yansp";
char *dbserver="10.223.18.116/yansp";
varchar v_object_name[40];//声明了一个VARCHAR类型的宿主变量,在程序中没有初始化其长度
exec sql end declare section;
exec sql connect :username identified by :password using :dbserver;
exec sql execute
begin
select object_name into :v_object_name
from his_test where rownum=1;
end;
end-exec;
printf("errmsg=[%s]\n",sqlca.sqlerrm.sqlerrmc);
printf("object_name=[%s]\n",v_object_name);
exec sql commit work release;
}
/home/cpicsrv/yansp > proc parse=none sqlcheck=semantic test.pc
Pro*C/C++: Release 10.2.0.1.0 - Production on 星期四 1月 12 14:48:37 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
系统默认选项值取自于: /home/oracle/oracle/product/10.2.0/client_1/precomp/admin/pcscfg.cfg
/home/cpicsrv/yansp > gcc $ORACLE_HOME/lib/libclntsh.so test.c -o test
/home/cpicsrv/yansp > ./test
errmsg=[ORA-01458: 内部变量字符串长度无效 ]
object_name=[]
运行将会导致ORA-01458错误。
程序修改如下,即可避免这个错误:
#include
#include
exec sql include sqlca;
main()
{
exec sql begin declare section;
char *username="yansp";
char *password="yansp";
char *dbserver="10.223.18.116/yansp";
varchar v_object_name[40];
exec sql end declare section;
exec sql connect :username identified by :password using :dbserver;
v_object_name.len=sizeof(v_object_name.arr); //初始化长度
exec sql execute
begin
select object_name into :v_object_name
from his_test where rownum=1;
end;
end-exec;
printf("errmsg=[%s]\n",sqlca.sqlerrm.sqlerrmc);
printf("object_name=[%s]\n",v_object_name.arr);
exec sql commit work release;
}
再次编译,就可以正常运行。
/home/cpicsrv/yansp > ./test
errmsg=[]
object_name=[/67db640c_IterConvertProfileIt]
/home/cpicsrv/yansp >
阅读(3433) | 评论(0) | 转发(0) |