How to get the return value of an Oracle function?

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
mdonath2
Posts: 3
Joined: Mon 04 Mar 2013 13:55

How to get the return value of an Oracle function?

Post by mdonath2 » Mon 04 Mar 2013 14:17

In a package 'UTILS' I have the function:

FUNCTION TestF(p_Name IN VARCHAR2) RETURN INTEGER;

In C++ Builder I call the function by:

OraQuery1->SQL->Add(L"DECLARE");
OraQuery1->SQL->Add(L" v_Res INTEGER;");
OraQuery1->SQL->Add(L"BEGIN");
OraQuery1->SQL->Add(L"v_Res := UTILS.TestF(:p_Name);");
OraQuery1->SQL->Add(L"END;");
OraQuery1->ParamByName(L"p_Name")->AsString = L"TestName";
OraQuery1->Execute();

All is OK. The function is executed correctly. But how to get the return parameter of the function? My test was:

OraQuery1->SQL->Add(L"BEGIN");
OraQuery1->SQL->Add(L":p_Res := UTILS.TestF(:p_Name);");
OraQuery1->SQL->Add(L"END;");
OraQuery1->ParamByName(L"p_Name")->AsString = L"TestName";
OraQuery1->Execute();
int res = OraQuery1->ParamByName(L"p_Res")->AsInteger;

But this fails with error EOraError::ORA-06502 ( numeric or value error: character string buffer too small)

I think I have to declare the return value before. But how to do?

Markus

jfudickar
Posts: 202
Joined: Fri 10 Mar 2006 13:03
Location: Oberursel / Germany

Re: How to get the return value of an Oracle function?

Post by jfudickar » Mon 04 Mar 2013 22:12

Without sitting at my compiler, I would say that you have to declare the parameter in this case before calling execute.
Have a look for declareparam or so.
Regards
Jens

mdonath2
Posts: 3
Joined: Mon 04 Mar 2013 13:55

Re: How to get the return value of an Oracle function?

Post by mdonath2 » Tue 05 Mar 2013 08:00

That's what I think. I have to declare the parameter first. But I can't find any function which looks like a 'Declare Param'-function.

Markus

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to get the return value of an Oracle function?

Post by AlexP » Tue 05 Mar 2013 09:16

Hello,

To solve the problem, you should specify the OUT parameter type:

OraQuery1->SQL->Add(L"BEGIN");
OraQuery1->SQL->Add(L":p_Res := UTILS.TestF(:p_Name);");
OraQuery1->SQL->Add(L"END;");
OraQuery1->ParamByName(L"p_Name")->AsString = L"TestName";
OraQuery1->ParamByName(L"p_Res")->DataType = ftInteger;
OraQuery1->ParamByName(L"p_Res")->ParamType = ptOutput;
OraQuery1->Execute();
int res = OraQuery1->ParamByName(L"p_Res")->AsInteger;

mdonath
Posts: 5
Joined: Tue 02 Oct 2012 14:03
Location: Leipzig, Germany

Re: How to get the return value of an Oracle function?

Post by mdonath » Tue 05 Mar 2013 12:38

Thank you. That is what I was looking for.

Markus

Post Reply