• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

Executing Oracle Stored Procedures with MFC and ODBC

Hello,

I have a simple stored procedure in Oracle 9i like this:

create or replace procedure Add_Numbers(var1 IN number,var2 IN number,var3 OUT number) as
begin
var3 := var1 + var2;
end;

I am trying to execute this procedure using MFC's CRecordset derived class like this (relevant portions only...)
--------------------------------------------------------
IMPLEMENT_DYNAMIC(AddNumbers, CRecordset)

AddNumbers::AddNumbers(CDatabase* pdb)
       : CRecordset(pdb)
{
      // Param Init
      m_Return_Value = -1;
      m_lNumber1 = 0;
      m_lNumber2 = 0;
      m_nParams = 3;

      m_nDefaultType = snapshot;
}

CString AddNumbers::GetDefaultSQL()
{
      return _T("{? = CALL Add_Numbers (?, ?)}");
}

void AddNumbers::DoFieldExchange(CFieldExchange* pFX)
{
      // RFX output param mapping
      pFX->SetFieldType(CFieldExchange::outputParam);
      RFX_Long(pFX, _T("[@var3]"), m_Return_Value);

      // RFX input param mapping
      pFX->SetFieldType(CFieldExchange::inputParam);
      RFX_Long(pFX, _T("[@var1]"), m_lNumber1);
      RFX_Long(pFX, _T("[@var2]"), m_lNumber2);
}

void AddNumbers::Move(long lRows, WORD wFetchType)
{
      m_bBOF = m_bEOF = TRUE; // By pass CRecordset
}

BOOL AddNumbers::ExecDirect()
{
      return Open(CRecordset::forwardOnly, NULL, CRecordset::executeDirect);
}

I call this class like this:

CDatabase db;
CString strConn = _T("DSN=ORADB90;UID=system;PWD=manager");
db.OpenEx(strConn,CDatabase::noOdbcDialog);
AddNumbers rs(&db);
rs.m_lNumber1 = 10;
rs.m_lNumber2 = 20;
BOOL bRet = rs.ExecDirect();
long ret = rs.m_Return_Value;

When I run this code, I am getting this error at the "ExecDirect" line:
----------------------------------------------------
ORA-24334: no descriptor for this position.
----------------------------------------------------

Please tell me what is that I am doing wrong?
(The same code works fine for a similar procedure in SQL Server!)

Thanks,

Balaji
0
BalajiS
Asked:
BalajiS
  • 2
1 Solution
 
rockiroadsCommented:
Using this, does it just use the MS ODBC Driver? or can you get it to use the Oracle ODBC Driver? If you can, try using the Oracle driver and see what happens
0
 
BalajiSAuthor Commented:
The DSN ORADB90 that I have used in my code actually uses an Oracle ODBC Driver.
0
 
sathisemailCommented:
Hi,
  The actual function call should be
         return _T("{? = CALL Add_Numbers (?, ?, ?)}");

Since the Add_Numbers procedure takes 3 parameters, you should pass 3 parameters when calling the function as well.
The return value is the result of executing the stored procedure and not the out parameter.

Hence the functions should be as follows.
void AddNumbers::DoFieldExchange(CFieldExchange* pFX)
{
     // RFX result param mapping
     pFX->SetFieldType(CFieldExchange::outputParam);
     RFX_Long(pFX, _T("RETURN_VALUE"), m_Result);

     // RFX input param mapping
     pFX->SetFieldType(CFieldExchange::inputParam);
     RFX_Long(pFX, _T("[@var1]"), m_lNumber1);
     RFX_Long(pFX, _T("[@var2]"), m_lNumber2);

     // RFX output param mapping
     pFX->SetFieldType(CFieldExchange::outputParam);
     RFX_Long(pFX, _T("[@var3]"), m_Return_Value);
}

CString AddNumbers::GetDefaultSQL()
{
     return _T("{? = CALL Add_Numbers (?, ?, ?)}");
}

I hope this should solve your problem.

regards
Sathis
0
 
BalajiSAuthor Commented:
Great answer! It works fine. With Microsoft ODBC for Oracle your solution works perfectly.

With the Oracle driver I had to make some small changes:

1. Change GetDefaultSQL like this:
return _T("{CALL Add_Numbers (?,?,?)}");

2. Change DoFieldExchange accordingly:

Thanks,

Balaji
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now