Avatar of Eddie Shipman
Eddie Shipman
Flag for United States of America asked on

Petapoco - Getting return from Oracle function

I'm running into the same problem as outlined in this post:  
http://stackoverflow.com/questions/35113140/executing-oracle-function-and-getting-back-a-return-value 

I can do this in my Toad client successfully:
declare result varchar2(30);
BEGIN 
  result:=WEBUSER.F_UpdateParticipant(json input_goes here);
  dbms_output.put_line(result); 
END;

Open in new window

and get the return value shown in dbms_output.
This function returns:
{"Success":true} 

or 

{"Success":false} 

Open in new window

But I cannot get the output returned to Petapoco. I've also tried using output params like this:
var result = new Oracle.ManagedDataAccess.Client.OracleParameter("result",Oracle.ManagedDataAccess.Client.OracleDbType.Varchar2, System.Data.ParameterDirection.Output);
var sql = "DECLARE result VARCHAR2(30);" + 
          "BEGIN "+
          "    @0:=WEBUSER.F_UpdateParticipant(@1);" +
          "END;";
_db.db.Execute(sql, result, json);
res = result.ToString();

Open in new window

AND
var result = new Oracle.ManagedDataAccess.Client.OracleParameter("result",Oracle.ManagedDataAccess.Client.OracleDbType.Varchar2, System.Data.ParameterDirection.Output);
var sql = "DECLARE result VARCHAR2(30);" + 
          "BEGIN "+
          "    @result:=WEBUSER.F_UpdateParticipant(@1);" +
          "END;";
_db.db.Execute(sql, result, json);
res = result.ToString();

Open in new window

Yes, used both Execute and ExecuteScalar with same results.  
I don't really want to go back to the ADO way of doing these types of queries.
C#.NET ProgrammingOracle Database

Avatar of undefined
Last Comment
Eddie Shipman

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

I think you can make your life much simpler ...
select WEBUSER.F_UpdateParticipant(@1) from dual;

Open in new window



and don't use the parameter stuff, but a plain "ExecuteScalar" method in "db", though I don't know what "_db" and "_db.db" objects are ...

object res = _db.db.ExecuteScalar(sql, json);

Open in new window

Eddie Shipman

ASKER
Guy,
The @0 is the first parameter in the parameters list in the function call, which is json, If I use @1, I get this error: Specified argument was out of the range of valid values.
I have already tried this this way too, but I MUST get the return value from the function call.
Doing it like above (with @0) returns this error: ORA-00911: invalid character

_db.db is the PetaPoco Db connector.

Do you have any other suggestions?
Eddie Shipman

ASKER
Ok, I've been able to get it to successfully update the record, however, I cannot get the return value at all.
string sql = "DECLARE res VARCHAR2(30); BEGIN return WEBUSER.F_UpdateParticipant(@0); END;";
object res  = _db.db.ExecuteScalar<string>(sql, json);

Open in new window


But res is null because I cannot return anything from the "procedure", I get this if I try:
ORA-06550: line 1, column 33:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 1, column 33:
PL/SQL: Statement ignored
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Guy Hengel [angelIII / a3]

this should do:
string sql = "select WEBUSER.F_UpdateParticipant(@0) FROM DUAL";
string res  = _db.db.ExecuteScalar<string>(sql, json);

Open in new window

Eddie Shipman

ASKER
Not exactly... Besides an Update, there is also an insert in the function so we get the cannot perform a DML operation inside a query exception.
Guy Hengel [angelIII / a3]

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Eddie Shipman

ASKER
I'll have to ask our Oracle developer about this one, for sure...
Eddie Shipman

ASKER
Ok, after adding that to the function, I'm getting this:
ORA-06519: active autonomous transaction detected and rolled back
ORA-06512: at "WEBUSER.F_UPDATEPARTICIPANT", line 153

Open in new window

I'm going to talk to the Oracle developer about how to rewrite this function to work correctly as it is, in my opinion, kind of clunky and stupid in the way it was written.
ASKER CERTIFIED SOLUTION
Eddie Shipman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Guy Hengel [angelIII / a3]

interesting ...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Eddie Shipman

ASKER
Yes, for some reason, I kept getting some stupid error from Oracle about invalid character "". so I rewrote the SQL without the wrapping and viola!!
Well, I also redid the output parameter creation, too.
Eddie Shipman

ASKER
Self-answered