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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
BalajiSAuthor Commented:
The DSN ORADB90 that I have used in my code actually uses an Oracle ODBC Driver.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.