简体   繁体   English

使用OCILIB调用带有输出变量的Oracle过程

[英]Calling a Oracle procedure with output variables using OCILIB

I have a procedure with the following signature: 我有一个带有以下签名的过程:

procedure countryExists(iCountryName in varchar2, oCount out integer)

When I run it using OCILIB, I can't get the right value for oCount. 当我使用OCILIB运行它时,我无法获得正确的oCount值。 If I register it as integer (using OCI_RegisterInt), I get the error: 如果我将其注册为整数(使用OCI_RegisterInt),则会收到错误消息:

ORA-03116: invalid buffer length passed to a conversion routine ORA-03116:无效的缓冲区长度传递给转换例程

If I register it as a string, it runs, but OCI_GetString returns a null pointer and OCI_GetInt returns 0 (instead of the expected result 1). 如果我将其注册为字符串,它将运行,但是OCI_GetString返回空指针,而OCI_GetInt返回0(而不是预期的结果1)。

The test code is: 测试代码为:

int init = OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT|OCI_ENV_CONTEXT);
OCI_Connection *cn = OCI_ConnectionCreate("mydb", "myuser", "mypass", OCI_SESSION_DEFAULT);
OCI_Statement *st = OCI_StatementCreate(cn);

int resultprepare = OCI_Prepare(st, "call mypackage.countryExists('BRAZIL', :oCount)");
//int registercount = OCI_RegisterString(st, ":oCount", 100);
int registercount= OCI_RegisterInt(st, ":oCount");
int executeresult = OCI_Execute(st);
OCI_Error *err1 = OCI_GetLastError();
const char *error1 = OCI_ErrorGetString(err1);
OCI_Resultset *resultset = OCI_GetResultset(st);
const wchar_t *valstr = OCI_GetString(resultset, 1);
int valint = OCI_GetInt(resultset, 1);
OCI_Error *err2 = OCI_GetLastError();
const char *error2 = OCI_ErrorGetString(err2);

Running the procedure using, for example, PL/SQL Developer works fine. 例如,使用PL / SQL Developer运行该过程可以正常工作。

Is this the right way of calling procedures using OCILIB? 这是使用OCILIB调用过程的正确方法吗?

Also note that I'm using the mixed version of the library. 另请注意,我使用的是库的混合版本。

You're not coding in Java.... 您不是用Java编写代码...。

Here is the right way to do it : 这是正确的方法:

OCI_Connection *cn;
OCI_Statement  *st;
int count;

OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT|OCI_ENV_CONTEXT);

cn = OCI_ConnectionCreate("mydb", "myuser", "mypass", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);

OCI_Prepare(st, "begin mypackage.countryExists('BRAZIL', :oCount); end;");
OCI_BindInt(st, ":oCount", &count);
OCI_Execute(st);

Check the OCILIB documentation and/or manual 检查OCILIB文档和/或手册

Vincent 文森特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM