ORA-06502 assigning values from SQL to PL/SQL variable

Source: Internet
Author: User

The results returned by the recent SQL query cause a ORA-06502 error to the PL/SQL variable. The error description is ORA-06502: PL/SQL: numeric or value error: character string buffer too small. obviously, the length of the character variable definition is not enough. It is added to 20 to 100. Continue to 06502. What is the problem?

1. Problem Description
-- The problem is that in a package, there are two parameter cursors, one parent cursor and one child cursor. When the output result of the parent cursor is passed to the child game tag, the system prompts that the value is too large.
-- The original SQL statement of the parent cursor is long and complex. to simplify the description, construct the environment

--> Oracle version
Goex_admin @ CNMMBO> select * from v $ version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Release 10.2.0.3.0-64bit Production
PL/SQL Release 10.2.0.3.0-Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0-Production
NLSRTL Version 10.2.0.3.0-Production

--> Create a test table t and insert 3 Records
Goex_admin @ CNMMBO> create table t (dt char (8 ));

Table created.

Goex_admin @ CNMMBO> insert into t select '000000' from dual;

1 row created.

Goex_admin @ CNMMBO> insert into t select '000000' from dual;

1 row created.

Goex_admin @ CNMMBO> insert into t select '000000' from dual;

1 row created.

Goex_admin @ CNMMBO> commit;

Commit complete.

--> Use the following query output results to report a ora-06502 Error
--> The query statement is also relatively simple. Take the minimum value of the dt column of Table t and assign the value to the variable in the outer query.
--> Sub-queries on the outer layer seem to be superfluous, purely simulating the original environment.
Goex_admin @ CNMMBO> DECLARE
2. tradedate_out CHAR (100 );
3 BEGIN
4 SELECT tradedate
5 INTO tradedate_out
6 FROM (select min (dt) AS tradedate FROM t) d;
7
8 DBMS_OUTPUT.Put_Line ('trade _ date = '| tradedate_out );
9 END;
10/
DECLARE
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4

2. Rewrite Query
--> No similar error occurs if we remove the outer query.
--> In this case, does the value cause a variation when the child queries the outer layer?
Goex_admin @ CNMMBO> DECLARE
2. tradedate_out CHAR (100 );
3 BEGIN
4 select min (dt) INTO tradedate_out FROM t;
5
6 DBMS_OUTPUT.Put_Line ('trade _ date = '| tradedate_out );
7 END;
8/
Trade _ date = 20121218

PL/SQL procedure successfully completed.

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.