Oracle Apex 22.21 - REST 数据源 - 嵌套 JSON 数组 - 通过触发器同步两个表 - PLSQL 错误问题

2024-03-26

这个问题是后续问题另一个问题 https://stackoverflow.com/questions/75192510/oracle-apex-rest-data-source-nested-json-array-sync-two-tables-where-to.

我实际上已经重新创建了上一个问题的表格。更新后的 JSON 响应可以在这个问题的底部找到。

ORDERS_LOCAL table

ORDERS_LOCAL表数据。ORDER_ITEMScolumn 是我需要提取到的 JSON 数组ORDER_ITEMS_LOCAL table.

ORDER_ITEMS_LOCAL table. LINE_ID列应该自动创建。ORDER_ID列是一个外键ORDERS_LOCAL table. PRODUCT_ID列是一个外键PRODUCTS table. LINE_NUMBER只是订单行号(第 1 行 = 产品 1、价格、数量 | 第 2 行 = 产品 2、价格、数量等...)我相信它被称为sequence type?

PRODUCTS table

PRODUCTS表数据

根据卡斯滕的回答,我为ORDERS表从对象浏览器.

然后我输入了上一个问题中 Carsten 的 PLSQL 代码。他确实提到这是伪代码。所以我尝试更新一下..

create or replace trigger "TR_MAINTAIN_LINES"
AFTER
insert or update or delete on "ORDERS_LOCAL"
for each row
begin
    if inserting then
        insert into ORDER_ITEMS_LOCAL ( line_id, order_id, line_number, product_id, quantity, price) 
        ( select :new.id,
                 seq_lines.nextval,
                 j.line_number,
                 j.product_id,
                 j.quantity,
                 j.price
            from json_table( 
                     :new.order_items,
                     '$[*]' columns (
                         line_number for ordinality,
                         product_id  number path '$.product_id',
                         quantity number        path '$.quantity',
                         price    number        path '$.price' ) ) );
    elsif deleting then
        delete ORDER_ITEMS_LOCAL
        where order_id = :old.id;
    elsif updating then
        delete ORDER_ITEMS_LOCAL
        where order_id = :old.id;
        -- 
        -- handle the update case here.
        -- I would simply delete and re-insert LINES rows.
    end if;
end;

我收到以下错误

Compilation failed, line 4 (08:38:57) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00049: bad bind variable 'NEW.ID'Compilation failed, line 19 (08:38:57) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00049: bad bind variable 'OLD.ID'Compilation failed, line 22 (08:38:57) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00049: bad bind variable 'OLD.ID'

我相信这是由于触发代码中缺少列造成的,但我不确定。

我是 PLSQL 新手,解析 JSON 有点令人困惑......尤其是下面。请看我的评论。

    if inserting then
        insert into ORDER_ITEMS_LOCAL ( line_id, order_id, line_number, product_id, quantity, price) 
        ( select :new.id,                  -- is this new id for `line_id`
                 order_id                  -- how to insert order_id foreign key
                 seq_lines.nextval,        -- not sure what this is for?
                 j.line_number,            -- I changed 'lines' to 'order_items' so should this be seq_order_items.nextval, ?
                 j.product_id,             
                 j.quantity,
                 j.price
            from json_table( 
                     :new.order_items,           -- I changed 'lines' to 'order_items' so I changed this from :new.lines,
                     '$[*]' columns (            -- Would I include 'line_id' and 'order_id' in here as well?
                         line_number for ordinality,
                         product_id  number path '$.product_id',
                         quantity number        path '$.quantity',
                         price    number        path '$.price' ) ) );

更新的 JSON 响应

[
{
    "order_id": "HO9b6-ahMY-B2i9",
    "order_number": 34795,
    "order_date": "2022-11-02",
    "store_id": 2,
    "full_name": "Ronda Perfitt",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "city": "Fresno",
    "state": "California",
    "zip_code": "93762",
    "credit_card": "5108758574719798",
    "order_items": [
      {
        "line_number": 1,
        "product_id": 2,
        "quantity": 1,
        "price": 3418.85
      },
      {
        "line_number": 2,
        "product_id": 7,
        "quantity": 1,
        "price": 4070.12
      }
    ]
  },
    {
    "order_id": "RFvUC-sN8Y-icJP",
    "order_number": 62835,
    "order_date": "2022-10-09",
    "store_id": 1,
    "full_name": "Wash Rosenfelt",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "city": "Chicago",
    "state": "Illinois",
    "zip_code": "60646",
    "credit_card": "5048372443777103",
    "order_items": [
      {
        "line_number": 1,
        "product_id": 1,
        "quantity": 1,
        "price": 3349.05
      },
      {
        "line_number": 2,
        "product_id": 3,
        "quantity": 1,
        "price": 4241.29
      },
      {
        "line_number": 3,
        "product_id": 1,
        "quantity": 1,
        "price": 3560.03
      }
    ]
  },
]

我很抱歉让这件事变得混乱。我真的很想学习如何正确地做到这一点。非常感谢您的支持。谢谢。


在触发器代码中,:old 和 :new 前缀引用触发器操作之前和之后的表行。所以 ...

  • 在更新的情况下,:old.{column-name}引用表列列的值before更新,:new.{column-name}引用值after更新。
  • 在 INSERTING 情况下,没有:old.{column-name}(因此该值为 NULL);:new.{column-name}引用插入的值。
  • 在 DELETING 情况下,没有:new.{column-name}价值;仅有的:old.{column-name}可用。

您会看到编译器错误,因为我的触发器伪代码包含:new.id,但您的表没有名为的列ID; it's ORDER_ID在你的情况下。所以你需要相应地调整该代码。

https://docs.oracle.com/en/database/oracle/oracle-database/21/lnpls/plsql-triggers.html#GUID-E76C8044-6942-4573-B7DB-3502FB96CF6F https://docs.oracle.com/en/database/oracle/oracle-database/21/lnpls/plsql-triggers.html#GUID-E76C8044-6942-4573-B7DB-3502FB96CF6F

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Oracle Apex 22.21 - REST 数据源 - 嵌套 JSON 数组 - 通过触发器同步两个表 - PLSQL 错误问题 的相关文章

随机推荐