我正在尝试使用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;
我怎样才能让它发挥作用?
答案 0 :(得分:1)
尝试使用数据类型VARCHAR2而不是VARCHAR
请参阅:onhashchange
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
答案 1 :(得分: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;