本文共 2321 字,大约阅读时间需要 7 分钟。
OTL很早前用过,今天写东西要调存储过程,程序写完了,调试死活通不过,折腾了一早晨。
最后才发现错误,这里总结一下:
有个参数后边少写了个“,”以至于总是抱错。而单独写的测试例子就没问题,后来一步一步跟踪了后才发现。
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> " ")}"
// 创建存储过程
/*
Create Or Replace Procedure Test(P1 In Number, P2 In Number, P3 Out Number) Is
BeginP3 := 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 calldb // 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 environmenttry{ //CString str_conn;db.rlogon(); //更换到对应值
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 exceptionscerr<<p.msg<<endl; // print out error messagecerr<<p.code<<endl; // print out error codecerr<<p.var_info<<endl; // print out the variable that caused the errorcerr<<p.sqlstate<<endl; // print out SQLSTATE messagecerr<<p.stm_text<<endl; // print out SQL that caused the error}db.logoff(); // disconnect from the data sourcegetchar();return 0;}转载地址:http://efkul.baihongyu.com/