我如何验证更新前后的行

时间:2019-04-07 15:29:42

标签: sql database oracle plsql

需要以某种方式进行编码,如果买方购买的数量超过库存控制台的数量,则应该显示一条消息,即您现在只能购买可用数量,剩余数量将很快更新,如果库存足以买它应该显示请继续

简而言之,“功能是在表order_line中插入一行之前验证现有数量并更新表库存中现有数量的过程”

CREATE OR REPLACE PACKAGE order_package IS
          global_inv_id NUMBER (6);
          global_quantity NUMBER (6);
    PROCEDURE create_new_order(current_c_id NUMBER,
 current_meth_pmt VARCHAR2, current_os_id NUMBER);
   PROCEDURE create_new_order_line(current_o_id NUMBER);
       END;
        /


CREATE OR REPLACE PACKAGE BODY order_package IS

    PROCEDURE create_new_order(current_c_id NUMBER,
 current_meth_pmt VARCHAR2, current_os_id NUMBER) AS
         current_o_id NUMBER;
       BEGIN
         SELECT order_seq.NEXTVAL
         INTO  current_o_id
         FROM   dual;
           INSERT INTO orders
     VALUES(current_o_id, sysdate,current_meth_pmt, current_c_id,
       current_os_id);
          COMMIT;
       create_new_order_line(current_o_id);
       END create_new_order;
   PROCEDURE create_new_order_line(current_o_id NUMBER)AS
       BEGIN
       INSERT INTO order_line
    VALUES(current_o_id,global_inv_id, global_quantity);
         COMMIT;
        END create_new_order_line;
       END;
        /

1 个答案:

答案 0 :(得分:1)

您没有显示库存表,并且您的程序似乎没有数量订购值,因此其中有些是推测。您可能想要做的是先更新该表,然后使用RETURNING INTO子句获取更新的清单。

UPDATE inventory SET global_quantity = global_quantity - order_quantity
 WHERE global_inv_id = current_c_id
RETURNING global_quantity INTO l_global_quantity;

IF l_global_quantity < 0 THEN
    ROLLBACK;
    raise_application_error( -20001, 'You ordered too much!' );
ELSE
    [... create order goes here ...]
END IF;

正在订购current_c_id的商品吗?这将引发异常,任何调用您的过程都应捕获该异常。如何向用户显示错误取决于所使用的应用程序层。