0

我正在尝试使用 sql developer 在 pl/sql 中创建一个对象。我只是在弄乱基础知识以掌握它。我不断收到错误

对变量“I”的无效引用

SET serveroutput ON


create or replace type conditions as object
(var_name varcher (100) ,
extract_method varchar(100),
default_value varchar (100),
idList varchar (100));


   DECLARE
condition conditions;
TYPE namesarray IS VARRAY(1) OF conditions; 
names namesarray := namesarray();--figure out why this is.
BEGIN
 condition := conditions('a', 'b', 'c', 'd');
 names.extend;
  names(names.last):= condition;
 FOR i IN names.FIRST .. names.LAST
  LOOP
     DBMS_OUTPUT.PUT_line(i.idList);
    END LOOP;
end;

我怎样才能让它工作?

4

2 回答 2

1
  1. 尝试使用数据类型 VARCHAR2 而不是 VARCHAR 请参阅:varchar 和 varchar2 有什么区别?

  2. FOR..LOOP 隐式迭代变量 I,在这种情况下,仅包含集合的当前索引。事实上,你应该使用这个变量作为你的集合的索引。

请考虑以下方法:

SET serveroutput ON;

--Use VARCHAR2 instead of VARCHAR
CREATE OR REPLACE type conditions
AS object
  (
    var_name varchar2(100),
    extract_method VARCHAR2(100),
    default_value  VARCHAR2(100),
    idList         VARCHAR2(100)
  ) ;
/


DECLARE
  condition conditions;
  TYPE namesarray IS VARRAY(1) OF conditions;
  names namesarray := namesarray() ;--figure out why this is.
BEGIN
  condition := conditions('a', 'b', 'c', 'd') ;
  names.extend;
  names(names.last) := condition;
  FOR i IN names.FIRST .. names.LAST
  LOOP
    DBMS_OUTPUT.PUT_line(names(i) .idList); -- use I as the index for your collection
  END LOOP;
END;
/

输出:

d
于 2016-03-29T16:57:27.273 回答
1

如完美所示,VARRAY 可以有另一种选择。这里也可以使用 TABLE 类型。所有集合类型之间的区别在https://community.oracle.com/thread/457834?start=0&tstart=0

希望这可以帮助。

SET serveroutput ON;
DECLARE
TYPE namesarray
IS
  TABLE OF conditions;
  names namesarray := namesarray() ;--NULL Constructor
BEGIN
  names:=namesarray(conditions('a', 'b', 'c', 'd'));
  FOR i IN names.FIRST .. names.LAST
  LOOP
    DBMS_OUTPUT.PUT_line(names(i).idList); -- use I as the index for your collection
  END LOOP;
END;
于 2016-03-30T06:39:56.290 回答