1

我正在尝试在 pl/sql 中编写一个差异过程。错误消息从不说明结构应该是什么样的,只是它期望一些其他符号,通常表示行尾。我也不断收到示例代码错误...

  1. 为什么它会在第一个错误处停止,而不是给出找到的所有错误的列表?
  2. CREATE TYPE EditCost,找到 EditCost,期待其他符号。
  3. cost_matrix INTEGER[,找到 [,期待其他符号。
  4. original.length,无效参考。
  5. cost_matrix[, 发现 [, 期待其他符号。
  6. 返回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.
    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;
/
4

2 回答 2

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
---------
于 2012-10-10T10:23:18.897 回答
0

我实际上设法通过使用 VARRAY 消除了错误。但它不允许动态调整大小(即 VARRAY(length(original)) 或 VARRAY(original.length) 是不允许的。所以现在如果有人给出大于 10 的参数它将失败,我也没有得到任何结果.希望“antwood := 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);
于 2012-10-10T12:13:25.060 回答