集合是一维结构。您可以通过创建元素也是集合的集合来实现多维数组。
这是一个如何实现二维数组(矩阵)的示例:
SQL> set serveroutput on;
SQL>
SQL> create or replace procedure Matrix(p_m in number, p_n in number)
2 is
3 type T_Array is table of integer;
4 type T_Matrix is table of T_Array;
5 l_Matrix T_Matrix := T_Matrix();
6 l_m varchar2(101);
7 begin
8 for i in 1..p_m
9 loop
10 l_m := '';
11 l_matrix.extend;
12 l_Matrix(i) := T_Array();
13 for y in 1..p_n
14 loop
15 l_matrix(i).extend;
16 l_matrix(i)(y) := y;
17 l_m := l_m || ' | ' || To_Char(l_matrix(i)(y));
18 end loop;
19 dbms_output.put_line(l_m);
20 end loop;
21 end;
22 /
Procedure created
SQL> exec matrix(5,5);
| 1 | 2 | 3 | 4 | 5
| 1 | 2 | 3 | 4 | 5
| 1 | 2 | 3 | 4 | 5
| 1 | 2 | 3 | 4 | 5
| 1 | 2 | 3 | 4 | 5
PL/SQL procedure successfully completed
SQL> exec matrix(5,7);
| 1 | 2 | 3 | 4 | 5 | 6 | 7
| 1 | 2 | 3 | 4 | 5 | 6 | 7
| 1 | 2 | 3 | 4 | 5 | 6 | 7
| 1 | 2 | 3 | 4 | 5 | 6 | 7
| 1 | 2 | 3 | 4 | 5 | 6 | 7
在此处和此处了解有关收藏的更多信息
对评论的回应
您也可以成功使用nested table
。因此,您的代码可能如下所示:
SQL> set serveroutput on;
SQL>
SQL> CREATE OR REPLACE PROCEDURE diff_string(original in varchar2, other in varchar2, result out varchar2)
2 is
3 type T_Array is table of integer;
4 type T_Matrix is table of T_Array;
5 cost_matrix T_Matrix := T_Matrix();
6 --antwoord T_Matrix;
7 l_res varchar2(301);
8 BEGIN
9 IF original is NULL THEN
10 result := other;
11 ELSIF other is NULL then
12 result := original;
13 else
14 for i in 1 .. length(original)
15 loop
16 l_res := '';
17 cost_matrix.extend;
18 cost_matrix(i) := T_Array();
19 for j in 1 .. length(other)
20 loop
21 cost_matrix(i).extend;
22 cost_matrix(i)(j) := i * j;
23 l_res := l_res || ' | ' || To_char(cost_matrix(i)(j));
24 end loop;
25 dbms_output.put_line(l_res);
26 end loop;
27 end if;
28 END diff_string;
29 /
Procedure created
SQL> variable res varchar2(31);
SQL> exec diff_string('String1', 'String2', :res);
| 1 | 2 | 3 | 4 | 5 | 6 | 7
| 2 | 4 | 6 | 8 | 10 | 12 | 14
| 3 | 6 | 9 | 12 | 15 | 18 | 21
| 4 | 8 | 12 | 16 | 20 | 24 | 28
| 5 | 10 | 15 | 20 | 25 | 30 | 35
| 6 | 12 | 18 | 24 | 30 | 36 | 42
| 7 | 14 | 21 | 28 | 35 | 42 | 49
PL/SQL procedure successfully completed
res
---------