Monday, June 13, 2011

Out VARCHAR2 parameters in PL/SQL

The PL/SQL takes the maximum size of output variable for VARCHAR2 from the calling block/procedure.

Consider the following example.

--
create or replace package test_pkg as
procedure test_val(i_num in number, o_expression out varchar2);
end test_pkg;
--
create or replace package body test_pkg as
procedure test_val(i_num in number, o_expression out varchar2) as
begin
  o_expression := LPAD('>', i_num);
end;
end;
--
declare
  l_expression VARCHAR2(5);
begin
  test_pkg.test_val(i_num => 5,
  o_expression => l_expression);
  dbms_output.put_line('Output:'||l_expression);
end;

Output: >

The value of l_expression is 5. so the maximum permissible size of o_expression is 5. If it increases as per the below execution, it will give an exception.

Error report:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "DEMO.TEST_PKG", line 5
ORA-06512: at line 4
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause:
*Action:

No comments:

Post a Comment