Sql – Character string buffer too small error in Oracle Stored Procedure

oraclesqlstored-procedures

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

It is happening at line 31, the line that contains out_cnt_tot := 0; I'm really not sure why there is anything wrong with that line. Another programmer created this procedure and I'm really not familiar with SQL procedures. Can anyone help me figure this out?

create or replace 
PROCEDURE                  "FIP_BANKREC_PREP" 
                   (
    in_file_date in varchar2,
    in_bank_code in varchar2,
    out_cnt_apx_miss_no out integer,
    out_cnt_prx_miss_no out integer,
    out_cnt_apx_no_mtch out integer,
    out_cnt_prx_no_mtch out integer,
    out_cnt_ap_dup out integer,
    out_cnt_pr_dup out integer,
    out_cnt_bad out integer,
    out_cnt_ap_load out integer,
    out_cnt_pr_load out integer,
    out_cnt_ap_not_load out integer,
    out_cnt_pr_not_load out integer,
    out_cnt_tot out integer,
    out_message out varchar2
    ) as

file_date date;
ap_acct_no varchar2(16);
pr_acct_no varchar2(16);

-- ------------------------------------------------------
--  begin logic
-- ------------------------------------------------------
begin

  file_date := to_date(in_file_date,'yyyymmdd');
  out_cnt_tot := 0;   --- THE ERROR IS ON THIS LINE ---
  out_message := 'Test Message';

  select brec_acct_code into ap_acct_no 
    from MSSU.zwkfi_bankrec_accts
    where brec_acct_bank = in_bank_code
      and brec_acct_type = 'AP';

  select brec_acct_code into pr_acct_no 
    from MSSU.zwkfi_bankrec_accts
    where brec_acct_bank = in_bank_code
      and brec_acct_type = 'PR';      

// The rest of the procedure...

Best Solution

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!).

Related Question