我有2个关联数组:v1index由二进制整数v2index通过varchar2。
type r1 is record
( c1 number
, c2 varchar2(64));
type t1 is table of r1 index by binary_integer;
v1 t1;
type t2 is table of varchar2(64) index by binary_integer;
v2 t2;
counter number := 0;
type r3 is record
( no_visits number);
type t3 is table of r3 index by varchar2(64);
v3 t3;
我想要一个列表(可能是另一个关联数组-v3,其中包含v2中定义的来自v1的城市,以及我访问过这些城市的次数(访问次数在v1中定义)。
我以为v3是VARCAHR2的索引(索引是城市的名称),并且只包含一个值,即访问次数。
是否有可能实现类似的内容:
begin
v1(1).c1 := 1990;
v1(1).c2 := 'PARIS';
V1(2).c1 := 2000;
V1(2).c2 := 'PARIS';
v1(3).c1 := 2001;
v1(3).c2 := 'PARIS';
v1(4).c1 := 1992;
v1(4).c2 := 'MADRID';
v1(5).c1 := 1994;
v1(5).c2 := 'LONDON';
v1(6).c1 := 1998;
v1(6).c2 := 'PRAGUE';
v2(1) := 'PARIS';
v2(2) := 'LONDON';
v2(3) := 'MADRID';
for i in 1 .. v1.count loop
for j in 1 .. v2.count loop
if v1(i).c2 = v2(j)
then
v3(v2(j).c2) := counter + 1;
end if;
end loop;
end loop;
end;
[通常,可以。在这种情况下,存在一些问题-例如,在v3(v2(j).c2) := counter + 1;
行中,c2
中没有v2
元素,因此您将在该处出现编译错误,并且在同一行中应遵循v3
引用.no_visits
表示,但一般来说,该语言肯定允许您在此处尝试执行的操作。
使用v3
中的值初始化v2
,略微修改计数循环并在最终循环中显示结果:
declare
type r1 is record ( c1 number, c2 varchar2(64));
type t1 is table of r1 index by binary_integer;
v1 t1;
type t2 is table of varchar2(64) index by binary_integer;
v2 t2;
type r3 is record( no_visits number);
type t3 is table of r3 index by varchar2(64);
v3 t3;
begin
v1(1).c1 := 1990;
v1(1).c2 := 'PARIS';
V1(2).c1 := 2000;
V1(2).c2 := 'PARIS';
v1(3).c1 := 2001;
v1(3).c2 := 'PARIS';
v1(4).c1 := 1992;
v1(4).c2 := 'MADRID';
v1(5).c1 := 1994;
v1(5).c2 := 'LONDON';
v1(6).c1 := 1998;
v1(6).c2 := 'PRAGUE';
v2(1) := 'PARIS';
v2(2) := 'LONDON';
v2(3) := 'MADRID';
for i in 1..v2.count loop
v3(v2(i)).no_visits := 0;
end loop;
for i in 1 .. v1.count loop
for j in 1 .. v2.count loop
if v1(i).c2 = v2(j) then
v3(v2(j)).no_visits := v3(v2(j)).no_visits + 1;
end if;
end loop;
end loop;
for i in 1..v2.count loop
dbms_output.put_line('City: '||v2(i));
dbms_output.put_line('Visited: '||v3(v2(i)).no_visits);
end loop;
end;
输出:
City: PARIS
Visited: 3
City: LONDON
Visited: 1
City: MADRID
Visited: 1