OTL调用Oracle存储过程

  • 时间:
  • 浏览:0
  • 来源:大发3D_大发3D官方

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

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

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;"

); // 也可以 直接用代码创建来测试用的过程

*/

otl_connect db; // connect object

#include <iostream>

using namespace std;

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

// 创建存储过程

int main()

{

otl_connect::otl_initialize(); // initialize environment

try{

//CString str_conn;

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

o>>c;

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

}

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

则用常规的形式:

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"

/*

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

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;

char b[31];

a、机会#define OTL_ORA9I // Compile OTL 4/OCI8编译

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;

}

#include <stdio.h>

#define OTL_ORA9I//OTL_ODBC // Compile OTL 4.0/ODBC

#include <otlv4.h>

Create Or Replace Procedure Test(P1 In Number, P2 In Number, P3 Out Number) Is

Begin

P3 := P1 + P2;

End Test;

int c;

b、机会用#define OTL_ODBC // Compile OTL 4.0/ODBC编译

end;

则过程调用采用:

{call my_proc("

              " :A<int,inout>, "

              " :B<char[31],out>, "

              " :C<char[31],in> "

              ")}"

o>>a>>b;

cout<<"A="<<a<<", B="<<b<<endl;

}

begin

int a=1;

int b=3;

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

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

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

);

//调用代码