开发者

Calling a Oracle procedure with output variables using OCILIB

开发者 https://www.devze.com 2023-04-11 06:04 出处:网络
I have a procedure with the following signature: procedure countryExists(iCountryName in varchar2, oCount out integer)

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. If I register it as integer (using OCI_RegisterInt), I get the error:

ORA-03116: invalid buffer length passed to a conversion routine

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).

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_Execu开发者_StackOverflow社区te(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.

Is this the right way of calling procedures using OCILIB?

Also note that I'm using the mixed version of the library.


You're not coding in 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

Vincent

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号