简体   繁体   English

我怎样才能在oracle中将参数作为varchar2

[英]how can I out a parameter as varchar2 in oracle

I am using oracle 10g 我正在使用oracle 10g

this is my package specification and body 这是我的包装规格和内容

CREATE OR REPLACE PACKAGE P_1 AS
    TYPE T_CURSOR IS REF CURSOR;
    PROCEDURE USP_1(SP_NUM OUT VARCHAR2);
END P_1;

CREATE OR REPLACE PACKAGE BODY P_1 AS
    PROCEDURE USP_1(SP_NUM OUT VARCHAR2) IS
    BEGIN
        SELECT to_char(concat(to_char(sysdate, 'yyyymm'),
                              to_char("ActionId".nextval, '000')))
            INTO SP_NUM FROM dual;
    END USP_1;
END P_1;

my back-end code is 我的后端代码是

using (OracleConnection oracleConnection = new BaseRepository().Connection)
{
    oracleConnection.Open();

    OracleCommand command = new OracleCommand("P_1.USP_1", oracleConnection);

    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.Add("SP_NUM", OracleDbType.Varchar2, 50,
                           System.Data.ParameterDirection.Output);

    command.ExecuteNonQuery();  // exception arises at this line

    var Number = Convert.ToInt32(command.Parameters["SP_REQ_NUM"].Value.ToString());

this is the exception i am getting. 这是我得到的例外。

ORA-06502: PL/SQL: numeric or value error

I don't know where i am doing something wrong. 我不知道我在哪里做错了。

There are no less than 10 overloads of the OracleParameterCollection.Add method. OracleParameterCollection.Add方法的重载不少于10个。 It looks like you have accidentally called the wrong one. 看来您不小心打错了电话。

I believe the one you want is Add(string, OracleDbType, int, object, ParameterDirection) , in which case you're just missing a value for the object parameter. 我相信您想要的是Add(string, OracleDbType, int, object, ParameterDirection) ,在这种情况下,您只是缺少object参数的值。 This parameter should contain an initial value for the Oracle parameter you're using. 此参数应包含您正在使用的Oracle参数的初始值。 In your case, however, the initial value doesn't matter as it's an out parameter. 但是,对于您而言,初始值无关紧要,因为它是out参数。 Add null after 50 and your stored procedure call should succeed. 50之后添加null ,您的存储过程调用应会成功。

The one you have called is Add(string, OracleDbType, object, ParameterDirection) . 您调用的是Add(string, OracleDbType, object, ParameterDirection) The size 50 has been interpreted as an initial value for the parameter. 大小50已解释为该参数的初始值。 I'm not sure how to interpret the error that Oracle returns ("numeric or value error") – that implies to me that Oracle has tried to convert a string to a number and failed. 我不确定如何解释Oracle返回的错误(“数字或值错误”)–对我而言,这意味着Oracle尝试将字符串转换为数字并失败。 Perhaps the value 50 overrides the type OracleDbType.Varchar2 and so Oracle expects a number rather than a string? 也许值50会覆盖类型OracleDbType.Varchar2 ,因此Oracle需要数字而不是字符串?

There were another couple of problems I found: 我发现了另外两个问题:

  • Should command.Parameters["SP_REQ_NUM"] be command.Parameters["SP_NUM"] ? command.Parameters["SP_REQ_NUM"]应该是command.Parameters["SP_NUM"]吗?
  • Your stored procedure doesn't return a number; 您的存储过程不返回数字; calling Convert.ToInt32 on a string such as 201405 001 will fail. 在诸如201405 001类的字符串上调用Convert.ToInt32将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM