如何使用循环向 VARRAY 添加值 [英] How to add values to a VARRAY using a loop

查看:77
本文介绍了如何使用循环向 VARRAY 添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VARRAY,我想使用循环向这个 VARRAY 添加元素.这是我迄今为止尝试过的.

I have a VARRAY and I want to add elements to this VARRAY by using a loop. This is what I have tried so far.

DECLARE
TYPE code_array_  IS VARRAY(26) OF VARCHAR2(6);
codes_ code_array_;

BEGIN
FOR i IN 1..26 LOOP    
    codes_(i) := dbms_random.string('U',6);
  END LOOP;
END;

上面的代码给了我一个错误

Above code gives me an error

ORA-06531:对未初始化集合的引用"

"ORA-06531: Reference to uninitialized collection"

推荐答案

如错误信息所述,您需要初始化集合变量:

As the error message says, you need to initialise the collection variable:

...
BEGIN
  codes_ := code_array_();
  ...

但是您还需要调整它的大小,或者每次循环使用一个扩展名:

But you also need to size it, either with a single extension each time around the loop:

  FOR i IN 1..26 LOOP    
    codes_.extend;
    ...

或者在开始之前一次性延期:

Or a one-off extension before you start:

...
BEGIN
  codes_ := code_array_();
  ...
  codes_.extend(26);
  FOR i IN 1..26 LOOP    
    ...

您也可以使用后扩展大小来控制循环,以再次节省硬编码 26:

You could also use the post-extend size to control the loop, to save hard-coding 26 again:

DECLARE
  TYPE code_array_ IS VARRAY(26) OF VARCHAR2(6);
  codes_ code_array_;
BEGIN
  codes_ := code_array_();
  codes_.extend(26);
  FOR i IN 1..codes_.count LOOP    
    codes_(i) := dbms_random.string('U',6);
  END LOOP;
END;
/

PL/SQL procedure successfully completed.

阅读有关集合的更多信息.

这篇关于如何使用循环向 VARRAY 添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆