【问题标题】:PL/SQL - Declare a record where keep records of a table and an operation?PL/SQL - 声明记录表和操作的记录?
【发布时间】:2018-10-17 15:35:03
【问题描述】:

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

  • 声明一条记录 LOCATIONS 表(LOCATION_ID, STREET_ADDRESS、POSTAL_CODE、CITY、STATE_PROVINCE 和 COUNTRY_ID) 和一个操作。

  • 声明一个数组,将位置记录保存为元素。

  • 通过填充您想要的每个位置的值来初始化数组 处理
  • 操作类型可以是“U”表示更新,“I”表示插入,“D”表示删除
  • 从头到尾遍历所有数组元素并执行以下 数组中定义的每个位置的逻辑:
    1. 如果操作类型为“U”,则使用值更新 LOCATIONS 表 来自阵列。如果在表上找不到位置 ID,则插入它 作为新记录。
    2. 如果操作类型为“I”,则将记录插入表中。价值观应该 来自数组
    3. 如果操作类型为“D”,则从表中删除该位置。
    4. 对于每个操作,在处理完成后显示一条消息
    5. 如果运算符类型不同于 U、I、D,则显示正确的 指示“无效操作”的消息。
    6. 提交所有事务

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

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;

【问题讨论】:

  • 发布您的完整代码,说明您是如何进行的以及您无法理解如何进行的地方。

标签: arrays plsql oracle12c record


【解决方案1】:

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

您可以看到“关联数组”只是一个 PL/SQL 集合类型。

就“操作”而言,我基于规范的理解是,它纯粹是位置记录本身的一个属性。我会做类似下面的东西:

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;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2012-01-23
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多