在PL / SQL脚本中获取字符串缓冲区太小的错误?

时间:2016-01-09 23:55:59

标签: sql oracle plsql oracle-sqldeveloper

我是PL / SQL的新手。

我正在编写一个小脚本来帮助我将集合加入数据库表。 Th集合将包含数千个ID,但在这里,为了简单起见,我保持它非常小。

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;

这是我得到的错误。

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

如果还有其他错误,请帮助我更正我的脚本。我将非常感激。

1 个答案:

答案 0 :(得分:1)

DBMS_RANDOM.STRING()有两个参数:

  • 第一个是单个字符,它指定随机生成的字符类型;和
  • 第二个是长度。

当您向第一个参数提供4个字符长度的值时,只会接受1个字符的长度值时,会产生错误。

您可能打算做的是:

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;

或更简单(并转换为数字,因为值看起来是数字):

...

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

  ...