Search code examples
oraclepetapoco

Getting return from Oracle function


I'm running into the same problem as outlined in this post:
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;

and get the return value shown in dbms_output. This function returns:

{"Success":true} 

or

{"Success":false} 

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();

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();

Yes, used both Execute and ExecuteScalar with same results. This function does an insert as well as an update so I CANNOT do SELECT WEBUSER.F_UpdateParticipant(json input_goes here) from DUAL; here because I'll get the cannot perform a DML operation inside a query exception. I don't really want to go back to the ADO way of doing these types of queries.


Solution

  • I had tried it before from looking at my previous post. However, when I wrote the SQL, I included the wrapped version and that is what caused the exception, the wrapped sql. If the SQL is written like below, it works, perfectly.

    public string UpdateParticipant(ParticipantUpdate Participant)
    {
        string ret = "";
        IsoDateTimeConverter dt = new IsoDateTimeConverter();
        dt.DateTimeFormat = "MM-dd-yyyy"; // we must have this format for our dates
        string json = JsonConvert.SerializeObject(Participant, dt);
        // Creating this output parameter is the key to getting the info back.
        var result = new OracleParameter
        {
            ParameterName = "RESULT",
            Direction = System.Data.ParameterDirection.InputOutput,
            Size = 100,
            OracleDbType = OracleDbType.Varchar2
        };
        // Now, setting the SQL like this using the result as the output parameter is what does the job.
        string sql = $@"DECLARE result varchar2(1000); BEGIN  @0 := WEBUSER.F_UpdateParticipant(@1); END;";
        var res = _db.db.Execute(sql, result, json);
    
        // Now return the value of the Output parameter!!
    
        ret = result.Value.ToString();
        return ret;
    }