Frage

I'm trying to execute the following stored procedure in .net 4.5

 PROCEDURE my_procedure ( 
      a_cursor          OUT t_cursor,
      return_value      OUT VARCHAR2,
      a_type                VARCHAR2,
      a_time                DATE,
      a_id                  VARCHAR2,
      a_arg1                VARCHAR2,
      a_from                VARCHAR2,
      a_gl2                 VARCHAR2,
      a_arg2                DATE DEFAULT NULL,
      a_templ               VARCHAR2 DEFAULT NULL 
      )

Most of these arguments can be null. In toad I can execute it with:

BEGIN package.my_procedure( :c, :out, 'arg', '', '123' , '', '', '', '', ''); END;

In C# I'm trying the following:

object[] parameters = {
         new OracleParameter("arg1", arg1),
         new OracleParameter("arg", "arg"),
         new OracleParameter("empti", ""),
         new OracleParameter("out", OracleDbType.Varchar2, ParameterDirection.Output),
         new OracleParameter("c", OracleDbType.RefCursor, ParameterDirection.Output)
                                      };

     const string sql = "BEGIN package.my_procedure  
          (:c, :out, :arg, :empti, :arg1 , :empti, :empti, :empti, :empti, :empti); END;";
     res = _projectRepository.ExecuteStoredProcedure(sql, parameters);

And I get the following error

Test method Tests.repository.RepositoryTests.TestMethod1 threw exception: 
Oracle.DataAccess.Client.OracleException: ORA-06550: 
line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'my_procedure'ORA-06550: 
line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'my_procedure'ORA-06550: 
line 1, column 7:
PL/SQL: Statement ignored

I've tried with and without the optional parameters and with/without the return_value/:out parameter.

I am using the Oracle.DataAccess provider v. 4.112.3.0. It works with other stored procedures that does not have that many parameters / only one out parameter (cursor).

EDIT: I found one error, i need to have the output parameters first in the parameter array;

new OracleParameter("c", OracleDbType.RefCursor, ParameterDirection.Output),
                    new OracleParameter("out", OracleDbType.Varchar2, ParameterDirection.Output),
                    new OracleParameter("arg1", "arg1"),
                    new OracleParameter("empti", ""),
                    new OracleParameter("arg", arg),
                                      };

Now i am getting the error:

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

seems like a database error, but looks strange since i can execute the same command from toad without getting the error.

War es hilfreich?

Lösung

When using VARCHAR2 as an OUPUT you need to provide the size of the character buffer. The constructor overloads won't help here though, you'll need to create the variable by hand to set the required properties.

// Specify a size adequate for the full return value.
OracleParameter returnValue =
  new OracleParameter("return_value", OracleDbType.Varchar2, 100);
returnValue.ParameterDirection = ParameterDirection.Output;

object[] parameters = {
  returnValue,
  // other parameters here
};

Original

I'll leave the original answer in tact, as it took care of the first version of the question.

The error provided is correct, you're not providing the right number of parameters in your call. Let's look at the original working code that Toad for Oracle can use and map them to the parameter names:

BEGIN
  package.my_procedure(a_cursor     => :c,
                       return_value => :out,
                       a_type       => 'arg',
                       a_time       => '',
                       a_id         => '123',
                       a_arg1       => '',
                       a_from       => '',
                       a_gl2        => '',
                       a_arg2       => '',  -- DEFAULT NULL
                       a_temp1      => ''); -- DEFAULT NULL
END;

Notice that in Toad for Oracle you're even providing values for the parameters declared as DEFAULT NULL (which isn't a problem).

If you look at your parameter list you'll see that you're providing bindings for arg1, arg, empti, out, and p. You position these to match up as follows:

a_cursor     => :c     -- you created the OracleParameter as 'p', not 'c'
return_value => :out  
a_type       => :arg
a_time       => :empti
a_id         => :arg1
a_arg1       => :empti
a_from       => :empti
a_gl2        => :empti
a_arg2       => :empti
a_temp1      => :empti

Oracle is complaining that the number of parameters didn't match, and that's because you're declaring your reference cursor parameter as p and not as c. You need to either update the OracleParamter to the name of c or change the PL/SQL being executed to :p for the first parameter.

Andere Tipps

You don't need to specify the out cursor as a parameter

other wise try the following code

object[] parameters = {
                                          new OracleParameter("arg1", arg1),
                                          new OracleParameter("arg", "arg"),
                                           new OracleParameter("empti", ""),
                                          new OracleParameter("return_value", OracleDbType.Varchar2, ParameterDirection.Output),
                                          new OracleParameter("a_cursor", OracleDbType.RefCursor, ParameterDirection.Output)
                                      };

                const string sql = "BEGIN package.my_procedure(:a_cursor, :return_value, :arg, :empti, :arg1 , :empti, :empti, :empti, :empti, :empti); END;";
                res = _projectRepository.ExecuteStoredProcedure(sql, parameters);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top