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
BalajiSAsked:
Who is Participating?
 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.