PL/SQL - 在保存 table 和操作的记录的地方声明一个记录?

PL/SQL - Declare a record where keep records of a table and an operation?

我目前正在尝试找出作业中的问题。我以前从未使用过数组,但我使用过集合、触发器、函数、过程和游标。我不是在寻求答案,而是在寻求一些帮助,因为我对如何处理它感到困惑。

关于操作的部分也让我感到困惑,因为我做了一些工作,你会在触发器中使用运算符,但它不涉及数组:

BEGIN
IF INSERTING THEN
    INSERT INTO carlog
    VALUES ('INSERT',user, SYSDATE, UPPER(:NEW.serial), UPPER(:NEW.make),
        UPPER(:NEW.model), UPPER(:NEW.color));
END IF;
IF UPDATING THEN
    INSERT INTO carlog
    VALUES ('UPDATE',user, SYSDATE,:old.serial, old.make, 
        old.model, old.color);
END IF;
IF DELETING THEN
    INSERT INTO carlog
    VALUES ('DELETE',user, SYSDATE,:old.serial, old.make, 
        old.model, old.color);
END IF;
END;

数组只是一种集合。如果您查看此处的文档:https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm#LNPLS00501

您可以看到 "Associative Array" 只是一个 PL/SQL 集合类型。

就"Operation"而言,我根据规范的理解是它纯粹是位置记录本身的一个属性。我会做类似下面的东西:

DECLARE 
  --declare your location record based on the spec
  TYPE location IS RECORD (
    location_id integer,
    operation VARCHAR2(1)
    --additional values from the spec would go here
  );

  --declare an array as a table of the location type
  TYPE location_array IS TABLE OF location INDEX BY BINARY_INTEGER;

  --two variables to hold the data
  example_location location;
  locations location_array;
BEGIN 
  --start building your location record for the location you want to modify
  example_location.location_id := 1;
  example_location.operation   := 'T';
  --..... additional values here

  --add the location to the array
  locations(locations.count + 1) := example_location;

  --repeat the above for any other locations, or populate these some other way

  --loop through the locations
  FOR i IN locations.first..locations.last
  LOOP
    --decide the logic based on the operation, could just as easily use your if logic here
    CASE locations(i).operation
      WHEN 'U' THEN
        dbms_output.put_line('your update logic here');
      WHEN 'I' THEN
        dbms_output.put_line('your insert logic here');
      WHEN 'D' THEN
        dbms_output.put_line('your delete logic here');
      ELSE
        dbms_output.put_line('other operations');
    END CASE;
  END LOOP;
END;

您需要调整以上内容以满足您的需要,添加相关逻辑、消息、提交、错误处理等。