OTL调用Oracle存储过程

OTL很早前用过,今天写东西要调存储过程,程序写完了,调试死活通不过,折腾了一早晨。

最后才发现错误,这里总结一下:

1、代码写的不规范。

有个参数后边少写了个“,”以至于总是抱错。而单独写的测试例子就没问题,后来一步一步跟踪了后才发现。

2、调用格式

a、如果#define OTL_ORA9I // Compile OTL 4/OCI8编译

则过程调用采用:

begin

过程名(:参数1<类型,in|out|inout>,:参数2<类型,in|out|inout>,.....);

end;

的形式,和在pl/sql中一样。

b、如果用#define OTL_ODBC // Compile OTL 4.0/ODBC编译

则用常规的形式:

{call my_proc("
              " :A<int,inout>, "
              " :B<char[31],out>, "
              " :C<char[31],in> "
              ")}"

 

3、附文档中给出的测试代码

// 创建存储过程

/*

Create Or Replace Procedure Test(P1 In Number, P2 In Number, P3 Out Number) Is
Begin
P3 := P1 + P2;
End Test;

*/

//调用代码

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ORA9I//OTL_ODBC // Compile OTL 4.0/ODBC
#include <otlv4.h>

#pragma comment(lib,"oci.lib")

otl_connect db; // connect object

void stored_proc1(void)
// invoking stored procedure

otl_stream o(1, // buffer size should be equal to 1 in case of stored procedure call
"begin my_proc("
":a1<int,inout>,"
":b1<char[31],out>, "
":c2<char[31],in> "
");end;",
// stored procedure call
db // connect object
);

o.set_commit(0); // set stream auto-commit off since
// the stream does not generate transaction 
o<<1<<"Test String1" // assigning :1 = 1, :3 = "Test String1"

int a;
char b[31];

o>>a>>b;
cout<<"A="<<a<<", B="<<b<<endl;
}

void stored_proc2(void)
// invoking stored procedure

char* call_sql = "Begin test("
":1<int,in>,"
":2<int,in>,"
":3<int,out>);end;"
otl_stream o(1,call_sql,db);

int a=1;
int b=3;

o<<a<<b; // assigning :1 = 1, :3 = "Test String1"

int c;

o>>c;
cout<<"A="<<a<<", B="<<b<<", C="<<c<<endl;
}

int main()
{
otl_connect::otl_initialize(); // initialize environment
try{
//CString str_conn;

db.rlogon(user/pwd@server); //更换到对应值

otl_cursor::direct_exec
(
db,
"CREATE OR REPLACE PROCEDURE my_proc "
" (A IN OUT NUMBER, "
" B OUT VARCHAR2, "
" C IN VARCHAR2) "
"IS "
"BEGIN "
" A := A+1; "
" B := C; "
"END;"
); // 也可以直接用代码创建来测试用的过程

stored_proc1(); // invoking stored procedure
stored_proc2();
}
catch(otl_exception& p)

// intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.code<<endl; // print out error code
cerr<<p.var_info<<endl; // print out the variable that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
}
db.logoff(); // disconnect from the data source
getchar();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值