PLSQL将n个参数传递给过程

时间:2012-11-29 16:18:33

标签: plsql

我正在做一项任务,需要一些如何回答这个问题的想法:

“允许用户通过输入n个订单商品(其中2≤n≤10)作为参数添加新订单”

我的问题是我可以使用什么来传递各种参数。然后,我需要检查内部存储这些内容以测试项目数量(参数传递)是> 2和< 10

为了插入新订单,我还需要让用户输入一个Cutomer No和数量的项目 - 这些参数是否需要在传递订单项目编号的同时传递?

2 个答案:

答案 0 :(得分:1)

您可以使用数组执行此操作,请参阅下文。您需要在数据库中创建类型和过程,然后运行脚本以查看其工作原理。 p_tab_order.COUNT命令可用于计算您拥有的订单数量。

CREATE TYPE  type_order AS OBJECT(item_number     NUMBER(6)
                                 ,customer_number NUMBER(6)
                                 ,quantity        NUMBER(6));


CREATE TYPE type_tab_order IS TABLE OF type_order;


CREATE OR REPLACE PROCEDURE p_order(p_tab_order IN type_tab_order)
IS
BEGIN

    FOR i IN 1..p_tab_order.COUNT LOOP


    dbms_output.put_line(p_tab_order(i).item_number||' item number '
                       ||p_tab_order(i).customer_number||' customer_number '
                       ||p_tab_order(i).quantity||' quantity '
                          );


    END LOOP;


END;


DECLARE 

l_tab_order type_tab_order := type_tab_order();

BEGIN

l_tab_order.EXTEND;
l_tab_order(1).item_number := 4;
l_tab_order(1).customer_number := 5;
l_tab_order(1).quantity := 6;

l_tab_order.EXTEND;
l_tab_order(2).item_number := 5;
l_tab_order(2).customer_number := 6;
l_tab_order(2).quantity := 7;

p_order(p_tab_order => l_tab_order);


END;

答案 1 :(得分:0)

可能有很多方法可以做到这一点。出于示例的目的,我假设您需要每个订单的客户编号和采购订单编号。在这种情况下你可以这样做:

PROCEDURE ADD_A_BUNCH_OF_ORDERS(CUST_NUM_1  IN NUMBER,
                                PO_NUM_1    IN NUMBER,
                                CUST_NUM_2  IN NUMBER,
                                PO_NUM_2    IN NUMBER,
                                CUST_NUM_3  IN NUMBER DEFAULT NULL,
                                PO_NUM_3    IN NUMBER DEFAULT NULL,
                                CUST_NUM_4  IN NUMBER DEFAULT NULL,
                                PO_NUM_4    IN NUMBER DEFAULT NULL,
                                CUST_NUM_5  IN NUMBER DEFAULT NULL,
                                PO_NUM_5    IN NUMBER DEFAULT NULL,
                                CUST_NUM_6  IN NUMBER DEFAULT NULL,
                                PO_NUM_6    IN NUMBER DEFAULT NULL,
                                CUST_NUM_7  IN NUMBER DEFAULT NULL,
                                PO_NUM_7    IN NUMBER DEFAULT NULL,
                                CUST_NUM_8  IN NUMBER DEFAULT NULL,
                                PO_NUM_8    IN NUMBER DEFAULT NULL,
                                CUST_NUM_9  IN NUMBER DEFAULT NULL,
                                PO_NUM_9    IN NUMBER DEFAULT NULL,
                                CUST_NUM_10 IN NUMBER DEFAULT NULL,
                                PO_NUM_10   IN NUMBER DEFAULT NULL)
IS
BEGIN
  ADD_ORDER(CUST_NUM_1, PO_NUM_1);
  ADD_ORDER(CUST_NUM_2, PO_NUM_2);

  IF CUST_NUM_3 IS NOT NULL AND
     PO_NUM_3 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_3, PO_NUM_3);
  END IF;

  IF CUST_NUM_4 IS NOT NULL AND
     PO_NUM_4 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_4, PO_NUM_4);
  END IF;

  IF CUST_NUM_5 IS NOT NULL AND
     PO_NUM_5 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_5, PO_NUM_5);
  END IF;

  IF CUST_NUM_6 IS NOT NULL AND
     PO_NUM_6 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_6, PO_NUM_6);
  END IF;

  IF CUST_NUM_7 IS NOT NULL AND
     PO_NUM_7 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_7, PO_NUM_7);
  END IF;

  IF CUST_NUM_8 IS NOT NULL AND
     PO_NUM_8 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_8, PO_NUM_8);
  END IF;

  IF CUST_NUM_9 IS NOT NULL AND
     PO_NUM_9 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_9, PO_NUM_9);
  END IF;

  IF CUST_NUM_10 IS NOT NULL AND
     PO_NUM_10 IS NOT NULL
  THEN
    ADD_ORDER(CUST_NUM_10, PO_NUM_10);
  END IF;
END ADD_A_BUNCH_OF_ORDERS;

然后,您只需要实现ADD_ORDER过程即可添加给定的特定顺序。

也许不是最优雅的方式,但 A 方式。 : - )

分享并享受。