0

我正在做一个作业,需要一些关于如何回答这个问题的想法:

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

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

为了插入新订单,我还需要让用户输入商品编号和数量 - 这些参数是否需要与传递订单商品编号同时传递?

4

2 回答 2

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;
于 2012-11-29T16:48:47.540 回答
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 过程来添加给定的特定顺序。

也许不是最优雅的方式,而是一种方式。:-)

分享和享受。

于 2012-12-01T02:54:30.587 回答