Character string buffer too small error in Oracle Stored Procedure

后端 未结 1 1767
面向向阳花
面向向阳花 2021-01-18 17:43

I am getting an error in an Oracle 11g stored procedure. The error is...

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

相关标签:
1条回答
  • 2021-01-18 18:11

    Simple demo of the scenario mentioned in comments:

    create or replace procedure p42(out_message out varchar2) as
    begin
      out_message := 'Test message';
    end p42;
    /
    

    If I call that with a variable that is declared big enough, it's fine. I have a 12-char variable, so assigning a 12-char value is not a problem:

    declare
      msg varchar2(12);
    begin
      p42(msg);
    end;
    /
    
    anonymous block completed
    

    But if I make a mistake and make the caller's variable too small I get the error you're seeing:

    declare
      msg varchar2(10);
    begin
      p42(msg);
    end;
    /
    
    Error report:
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at "STACKOVERFLOW.P42", line 3
    ORA-06512: at line 4
    06502. 00000 -  "PL/SQL: numeric or value error%s"
    *Cause:    
    *Action:
    

    The error stack shows both the line in the procedure that errored (line 3), and the line in the caller that triggered it (line 4). Depending on where you're calling it you might not have the whole stack, of course.

    You mentioned that there would be various error messagesin the future. You need to make sure that anything that ever calls this defines the variables to be big enough to cope with any of your messages. If they were stored in a table you could semi-automate that, otherwise it'll be a manual code review check.


    OK, saw your c# comment after posting this. It looks like you're calling this constructor; that doesn't say what default size it gets, but not unreasonable to think it might be 1. So you need to call this constructor instead to specify the size explicitly:

    OracleParameter(String, OracleType, Int32)
    Initializes a new instance of the OracleParameter class that uses the parameter name, data type, and length.

    ... something like:

    OracleParameter prm15 = new OracleParameter("out_str_message",
        OracleDbType.Varchar2, 80);
    

    Unless there's a way to reset the size after creation, which I can't see. (Not something I've ever used!).

    0 讨论(0)
提交回复
热议问题