qt调用oracle存储过程,该怎么处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt调用oracle存储过程,该怎么处理相关的知识,希望对你有一定的参考价值。

qt调用oracle存储过程是通过QsqlQuery来实现的。
用法举例:
QSqlQuery movementQuery ;
movementQuery.prepare("call Qt.add_movement(:pDocumentType , :pDocumentId ,
to_date(sysdate,\'dd-mm-yyyy\') ,:pDocumentNumber"
",to_date(sysdate,\'dd-mm-yyyy\') , :pCustId ,:pMovementId ,:pReturn )");
movementQuery.bindValue(":pDocumentType",documentType);
movementQuery.bindValue(":pDocumentId",documentId);
movementQuery.bindValue(":pDocumentNumber",0);
movementQuery.bindValue(":pCustId",ui->custId->text());
movementQuery.bindValue(":pMovementId", 0, QSql::Out);
movementQuery.bindValue(":pReturn", "FALSE", QSql::Out);
movementQuery.exec();
//// The query executed the query is active and no errors are valid
//// message is method to display the value
message(query.boundValue(":pReturn").toString());
message(query.boundValue(5).toString());
message(query.boundValue(":pMovementId").toString());
message(query.boundValue(4).toString());
参考技术A QT可能不支持这个功能 - Accessing REF CURSOR in Stored Procedure from Qt。
可以通过ODBC API来做,同样是跨平台的
The following sample application shows how to return a result set by using Oracle ODBC Driver:

/*
* Sample Application using Oracle reference cursors through ODBC
*
* Assumptions:
*
* 1) Oracle Sample database is present with data loaded for the EMP table.
*
* 2) Two fields are referenced from the EMP table, ename and mgr.
*
* 3) A data source has been setup to access the sample database.
*
*
* Program Description:
*
* Abstract:
*
* This program demonstrates how to return result sets using
* Oracle stored procedures
*
* Details:
*
* This program:
* Creates an ODBC connection to the database.
* Creates a Packaged Procedure containing two result sets.
* Executes the procedure and retrieves the data from both result sets.
* Displays the data to the user.
* Deletes the package then logs the user out of the database.
*
*
* The following is the actual PL/SQL this code generates to
* create the stored procedures.
*
DROP PACKAGE ODBCRefCur;
CREATE PACKAGE ODBCRefCur AS
TYPE ename_cur IS REF CURSOR;
TYPE mgr_cur IS REF CURSOR;
PROCEDURE EmpCurs(Ename IN OUT ename_cur, Mgr IN OUT mgr_cur, pjob IN VARCHAR2);

END;

/
CREATE PACKAGE BODY ODBCRefCur AS
PROCEDURE EmpCurs(Ename IN OUT ename_cur, Mgr IN OUT mgr_cur, pjob IN VARCHAR2)
AS
BEGIN
IF NOT Ename%ISOPEN
THEN
OPEN Ename for SELECT ename from emp;
END IF;

IF NOT Mgr%ISOPEN
THEN
OPEN Mgr for SELECT mgr from emp where job = pjob;
END IF;
END;
END;
/

*
* End PL/SQL for Reference Cursor.
*/

/*
* Include Files
*/
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
/*
* Defines
*/
#define JOB_LEN 9
#define DATA_LEN 100
#define SQL_STMT_LEN 500
/*
* Procedures
*/
void DisplayError( SWORD HandleType, SQLHANDLE hHandle, char *Module );
/*
* Main Program
*/
int main()

SQLHENV hEnv;
SQLHDBC hDbc;
SQLHSTMT hStmt;
SQLRETURN rc;
char *DefUserName ="scott";
char *DefPassWord ="tiger";
SQLCHAR ServerName[DATA_LEN];
SQLCHAR *pServerName=ServerName;
SQLCHAR UserName[DATA_LEN];
SQLCHAR *pUserName=UserName;
SQLCHAR PassWord[DATA_LEN];
SQLCHAR *pPassWord=PassWord;
char Data[DATA_LEN];
SQLINTEGER DataLen;
char error[DATA_LEN];
char *charptr;
SQLCHAR SqlStmt[SQL_STMT_LEN];
SQLCHAR *pSqlStmt=SqlStmt;
char *pSalesMan = "SALESMAN";
SQLINTEGER sqlnts=SQL_NTS;
/*
* Allocate the Environment Handle
*/
rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );
if (rc != SQL_SUCCESS)

printf( "Cannot Allocate Environment Handle\n");
printf( "\nHit Return to Exit\n");
charptr = gets ((char *)error);
exit(1);

/*
* Set the ODBC Version
*/
rc = SQLSetEnvAttr( hEnv,SQL_ATTR_ODBC_VERSION,(void *)SQL_OV_ODBC3,0);
if (rc != SQL_SUCCESS)本回答被提问者和网友采纳

oracle 存储过程中多线程执行另一个存储过程

我想在一个存储过程A中,调用另一个存储过程B,由于B是用来将某个表的数据插入到另一个表中,用游标根据年份来做的,为了节省时间就需要多线程并发执行,也就是要并发的调用存储过程B,这个sql该怎么写呢,用 execute immediate 'exec procedure B(2014);exec procedure B(2015);' 这样的方式?不知道该怎么写这个sql,求教!

参考技术A --可以参考如下:
DECLARE
CURSRO C_T IS

SELECT 年份 FROM 表名;
BEGIN
FOR C_C IN C_T LOOP

BEGIN

PROCEDURE B(C_C.年份);

END;

END LOOP;
END;追问

这样就只是循环来顺序执行而已,我要的是并发,就像在pl/sql里面开多个 command窗口,或者开多个sqlplus窗口来执行一样,我记得好像在linux的shell脚本里面有这种写法,但是忘了咋写了!

追答

shell脚本里好像是:exec PROCEDURE B(年份1) & exec PROCEDURE B(年份2) & exec PROCEDURE B(年份3)
你试试。。。

以上是关于qt调用oracle存储过程,该怎么处理的主要内容,如果未能解决你的问题,请参考以下文章

oracle中,一个存储过程怎么调用另一个存储过程?

oracle存储过程中临时表的使用,该怎么处理

九、存储过程中异常的处理

如何php调用oracle存储过程返回的是一个结果集,该怎么从php页面中吧数据循环输出呀

oracle存储过程中临时表的使用,该怎么处理

请问如何用c++ 调用oracle存储过程?