我正在尝试在pl / sql中编写diff过程。错误消息永远不会说明结构应该是什么样的,只是它期望一些其他符号,通常表示该行的结尾。此外,我不断收到示例代码的错误......
这段代码给了我很多错误,我删除了部分只是为了找到其他部分的错误。
CREATE OR REPLACE PROCEDURE diff_string(original in varchar2, other in varchar2, result out varchar2)
is -- Maybe should be as, don't know.
CREATE TYPE EditCost { 'Change', 'Copy', 'Delete', 'Insert', 'Kill' };
cost_matrix INTEGER[original.length, other.length];
BEGIN
IF original = NULL THEN
result := other;
ELSIF other = NULL then
result := original;
else
for i in 1 .. length(original)
loop
for j in 1 .. length(other)
loop
cost_matrix[i, j] := i * j;
end loop;
end loop;
return cost_matrix;
end if;
END diff_string;
/
答案 0 :(得分:2)
收藏品是一维结构。您可以通过创建其元素也是集合的集合来实现多维数组。
以下是如何实现二维数组(矩阵)的示例:
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
---------
答案 1 :(得分:0)
我实际上设法通过使用VARRAY删除错误。但它不允许动态大小调整(即VARRAY(长度(原始))或VARRAY(original.length)是不允许的。所以现在如果有人给出大于10的参数它将失败,我也没有得到任何结果希望“antwoord:= cost_matrix”会让它显示结果值。是否有其他方法可以做到这一点?
CREATE OR REPLACE PROCEDURE diff_string(original in varchar2, other in varchar2, result out varchar2)
is -- Maybe should be as, don't know.
type T_Array is VARRAY(10) of integer;
type T_Matrix is VARRAY(10) of T_Array;
cost_matrix T_Matrix := T_Matrix(null);
antwoord T_Matrix;
BEGIN
IF original = NULL THEN
result := other;
ELSIF other = NULL then
result := original;
else
for i in 1 .. length(original)
loop
cost_matrix.extend;
cost_matrix(i) := T_Array(null);
for j in 1 .. length(other)
loop
cost_matrix(i).extend;
cost_matrix(i)(j) := i * j;
end loop;
end loop;
antwoord := cost_matrix;
end if;
END diff_string;
/
CALL diff_string('hoi', 'hob', :verschil);