Web Services 带有标题和行输入的ORDS Web服务

h4cxqtbf  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(98)

我试图调用一个PLSQL过程,该过程有一个记录(标题)和一个表类型(行)作为输入参数。如果需要,我可以更改该过程,但在一天结束时,我需要创建一个带有标题和行的项目。我在网上看到一些文章,其中一个提到表类型不允许在ORDS中,只允许VARRAYS。这是真的吗?我如何实现这一点,或者有什么工作吗?
我的处理程序源看起来像这样。这是因为记录和表类型没有正确传递而出错。

DECLARE
    x_return_code NUMBER;
    x_return_msg VARCHAR2(200);
BEGIN
    xx.xx_manage_item.create_item(   p_req_id        => :req_id,
                                     p_header_rec    => :header_rec,
                                     p_sf_lines_tab  => :lines_tab,
                                     x_return_code   => x_return_code,
                                     x_return_msg    => x_return_msg);
END;

我的帖子请求如下所示:行可以是多行

{
   "req_id":1,
   "header_rec":{
      "cust_account_id":"123",
      "order_type":"XX",
      "customer_po":"TestPO2",
      "sales_person":"",
      "currency_code":"USD",
      "end_user_address":"Stockport 12",
      "request_date":"09/10/2023"
   },
   "lines_tab":[
      {
         "inv_item_id":"112",
         "item_name":"",
         "quantity":"1",
         "uom":"EA",
         "plant_number":"w12",
         "request_date":"09/10/2023"
      }
   ]
}

Thanks in advance

3yhwsihp

3yhwsihp1#

不要尝试将值拆分为header和lines,只需将整个JSON消息传递给过程并在那里处理它:
例如,您可以像这样创建过程:

CREATE PROCEDURE create_item(
  p_msg         IN  CLOB,
  x_return_code OUT NUMBER,
  x_return_msg  OUT VARCHAR2
)
IS
  v_req_id          NUMBER;
  v_cust_account_id VARCHAR2(200);
  v_order_type      VARCHAR2(200);
  v_customer_po     VARCHAR2(200);
  -- ...
BEGIN
  SELECT req_id,
         cust_account_id,
         order_type,
         customer_po
         -- ...
  INTO   v_req_id,
         v_cust_account_id,
         v_order_type,
         v_customer_po
         -- ...
  FROM   JSON_TABLE(
           p_msg,
           '$'
           COLUMNS (
             req_id          NUMBER        PATH '$.req_id',
             cust_account_id VARCHAR2(200) PATH '$.header_rec.cust_accout_id',
             order_type      VARCHAR2(200) PATH '$.header_rec.order_type',
             customer_po     VARCHAR2(200) PATH '$.header_rec.customer_po'
             -- ...
           )
         );
  DBMS_OUTPUT.PUT_LINE('req_id: ' || v_req_id);
  DBMS_OUTPUT.PUT_LINE('cust_account_id: ' || v_cust_account_id);
  DBMS_OUTPUT.PUT_LINE('order_type: ' || v_order_type);
  DBMS_OUTPUT.PUT_LINE('customer_po: ' || v_customer_po);
  -- ...

  FOR line IN (
    SELECT inv_item_id,
           item_name,
           quantity
           -- ...
    FROM   JSON_TABLE(
             p_msg,
             '$.lines_tab[*]'
             COLUMNS (
               inv_item_id VARCHAR2(200) PATH '$.inv_item_id',
               item_name   VARCHAR2(200) PATH '$.item_name',
               quantity    VARCHAR2(200) PATH '$.quantity'
               -- ...
             )
           )
  ) LOOP
    DBMS_OUTPUT.PUT_LINE('inv_item_id: ' || line.inv_item_id);
    DBMS_OUTPUT.PUT_LINE('item_name: ' || line.item_name);
    DBMS_OUTPUT.PUT_LINE('quantity: ' || line.quantity);
  END LOOP;
END;
/

然后传递你的信息:

DECLARE
  v_return_code NUMBER;
  v_return_msg  VARCHAR2(4000);
BEGIN
  DBMS_OUTPUT.ENABLE();
  create_item(
    p_msg => '{
   "req_id":1,
   "header_rec":{
      "cust_account_id":"123",
      "order_type":"XX",
      "customer_po":"TestPO2",
      "sales_person":"",
      "currency_code":"USD",
      "end_user_address":"Stockport 12",
      "request_date":"09/10/2023"
   },
   "lines_tab":[
      {
         "inv_item_id":"112",
         "item_name":"",
         "quantity":"1",
         "uom":"EA",
         "plant_number":"w12",
         "request_date":"09/10/2023"
      }
   ]
}',
    x_return_code => v_return_code,
    x_return_msg  => v_return_msg
  );
END;
/

其输出:

req_id: 1
cust_account_id: 
order_type: XX
customer_po: TestPO2
inv_item_id: 112
item_name: 
quantity: 1

fiddle

erhoui1w

erhoui1w2#

找到了问题的解决方案。我使用body_text参数将整个JSON捕获为CLOB对象,并将其作为参数传递到我的包中。我在包中解析了完整的JSON。
所以,我们不能在ORDS中使用表类型,但我们可以使用这种方法,所以我们不必这样做。下面是官方文档:
Oracle文档

相关问题