简体   繁体   中英

Getting character string buffer too small error in PL/SQL script?

I am a newbie in PL/SQL.

I am writing a small script that will help me join a collection to a database table. Th collection will contain thousands of IDs, but here, for the sake of simplicity, i have kept it very small.

VARIABLE cursor REFCURSOR;
DECLARE
  your_collection SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
BEGIN
  your_collection.EXTEND(2);
  FOR i IN 1 .. 2 LOOP
    your_collection(i) := DBMS_RANDOM.STRING( '7839', '7689' );
  END LOOP;
  OPEN :cursor FOR
  SELECT t.*
  FROM   emp t
         INNER JOIN
         TABLE( your_collection ) c
         ON t.empno = c.COLUMN_VALUE;
END;
/
PRINT cursor;

Here is the error im getting.

ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "SYS.DBMS_RANDOM", line 144
ORA-06512: at line 6
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    An arithmetic, numeric, string, conversion, or constraint error
           occurred. For example, this error occurs if an attempt is made to
           assign the value NULL to a variable declared NOT NULL, or if an
           attempt is made to assign an integer larger than 99 to a variable
           declared NUMBER(2).
*Action:   Change the data, how it is manipulated, or how it is declared so
           that values do not violate constraints.

CURSOR

Help me correct my script if there are other errors as well. I will be really grateful.

DBMS_RANDOM.STRING() takes two arguments:

  • the first is a single character which specifies the type of characters which are randomly generated; and
  • the second is the length.

The error is being produced as you are supplying a 4-character length value to the first argument when it will only accept a 1-character length value.

What you are probably intending to do is:

VARIABLE cursor REFCURSOR;

DECLARE
  your_collection SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
BEGIN
  your_collection.EXTEND(2); -- Set the length of the array
  your_collection(1) := '7839'; -- Specify the first value in the array.
  your_collection(2) := '7689'; -- Specify the second value in the array.

  OPEN :cursor FOR
  SELECT t.*
  FROM   emp t
         INNER JOIN
         TABLE( your_collection ) c
         ON t.empno = c.COLUMN_VALUE;
END;
/

PRINT cursor;

or more simply (and converted to numbers since the values appear to be numeric):

...

DECLARE
  your_collection SYS.ODCINUMBERLIST := SYS.ODCINUMBERLIST( 7839, 7689 );
BEGIN    
  OPEN :cursor FOR

  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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