如何在plsql中初始化矩阵

时间:2017-08-08 09:10:04

标签: oracle matrix plsql hashmap associative-array

我尝试使用关联数组映射表,但我无法弄清楚如何初始化它

这是一个例子:

TYPE RecType IS RECORD
 (
  value1   NUMBER,
  value2   NUMBER,
  value3   NUMBER
 );
 TYPE TblType IS TABLE OF RecType INDEX BY PLS_INTEGER;
 TYPE TblOfTblType IS TABLE OF TblType INDEX BY PLS_INTEGER;
 matrix   TblOfTblType;

现在,当我尝试像这样初始化矩阵时:

FOR i IN matrix.FIRST .. matrix.LAST LOOP
  FOR j IN matrix (i).FIRST .. matrix (i).LAST LOOP
       matrix(i)(j) := NULL;  
  END LOOP;
END LOOP;

它不起作用!我也试过

matrix := TblOfTblType()();

它显示以下错误:

  

PLS-00363表达式'Matrix'不能用作分配目标。

3 个答案:

答案 0 :(得分:4)

您不需要使用关联数组 - 集合将起作用。

DECLARE
  TYPE RecType IS RECORD
  (
    value1   NUMBER,
    value2   NUMBER,
    value3   NUMBER
  );
  TYPE TblType IS TABLE OF RecType;
  TYPE TblOfTblType IS TABLE OF TblType;
  matrix   TblOfTblType := TblOfTblType();
BEGIN
  matrix.EXTEND(3);
  FOR i IN 1 .. matrix.COUNT LOOP
    matrix(i) := TblType();
    matrix(i).EXTEND(4);
    FOR j IN 1 .. matrix(i).COUNT LOOP
      matrix(i)(j).value1 := i;
      matrix(i)(j).value2 := j;
      matrix(i)(j).value3 := DBMS_RANDOM.VALUE;
    END LOOP;
  END LOOP;

  FOR i IN 1 .. matrix.COUNT LOOP
    FOR j IN 1 .. matrix(i).COUNT LOOP
      DBMS_OUTPUT.PUT( '[' || matrix(i)(j).value1
                    || ',' || matrix(i)(j).value2
                    || ',' || matrix(i)(j).value3 || ']' || CHR(11) );
    END LOOP;
    DBMS_OUTPUT.NEW_LINE;
  END LOOP;
END;
/

答案 1 :(得分:3)

虽然我同意@MTO的方法,但是你的循环方法的错误是因为你试图在空表上引用FIRSTLAST,并且它们在那时都评估为null 。你正在努力做到:

FOR i IN null .. null LOOP

与原始ORA-06502: PL/SQL: numeric or value error循环相同FOR

您还没有定义矩阵的尺寸应该是什么。你需要在某个地方做到这一点才能“初始化”它,例如使用固定值来匹配MTO和您的类型声明:

DECLARE
  TYPE RecType IS RECORD
  (
    value1   NUMBER,
    value2   NUMBER,
    value3   NUMBER
  );
  TYPE TblType IS TABLE OF RecType INDEX BY PLS_INTEGER;
  TYPE TblOfTblType IS TABLE OF TblType INDEX BY PLS_INTEGER;
  matrix   TblOfTblType;
BEGIN
  FOR i IN 1 .. 3 LOOP
    FOR j IN 1 .. 4 LOOP
      matrix(i)(j) := null;
    END LOOP;
  END LOOP;
END;
/

PL/SQL procedure successfully completed.

要填充非空值,您需要一个单独的记录变量,然后填充矩阵位置:

  FOR i IN 1 .. 3 LOOP
    FOR j IN 1 .. 4 LOOP
      rec.value1 := i;
      rec.value2 := j;
      rec.value3 := DBMS_RANDOM.VALUE;
      matrix(i)(j) := rec;
    END LOOP;
  END LOOP;

或者你可以在没有循环的情况下定位特定的矩阵元素,如果你在其他地方获得这些形式的话。您可以按照MTO显示的方式打印出来,也可以使用现在有效的FIRSTLAST打印出来:

  FOR i IN matrix.FIRST .. matrix.LAST LOOP
    FOR j IN matrix(i).FIRST .. matrix(i).LAST LOOP
      DBMS_OUTPUT.PUT( '[' || matrix(i)(j).value1
                    || ',' || matrix(i)(j).value2
                    || ',' || matrix(i)(j).value3 || ']' || CHR(11) );
    END LOOP;
    DBMS_OUTPUT.NEW_LINE;
  END LOOP;

答案 2 :(得分:1)

CREATE OR REPLACE TYPE RecType AS OBJECT (
  value1   NUMBER,
  value2   NUMBER,
  value3   NUMBER
 );
/   

CREATE OR REPLACE TYPE TblType IS TABLE OF RecType;
/

CREATE OR REPLACE TYPE TblOfTblType IS TABLE OF TblType;
/

DECLARE
     matrix   TblOfTblType;   
       rec1   RecType;
       rec2   TblType; 
BEGIN
    rec1 := RecType(1,2,3);
    rec2 := TblType(rec1);   
    matrix := TblOfTblType(rec2);
    dbms_output.put_line(matrix(1)(1).value2);
END;