0

我目前有一个删除客户的程序,这是一个位于包体内的相当简单的程序。以下是运行和删除客户的过程的代码:

PROCEDURE remove_customer (customer_id VARCHAR2) IS
BEGIN
DELETE FROM order_line
WHERE  order_line.FK1_order_id in
(SELECT order_id FROM placed_order
 WHERE placed_order.FK1_customer_id = remove_customer.customer_id
);
DELETE FROM placed_order
WHERE placed_order.FK1_customer_id = remove_customer.customer_id;
DELETE FROM customer
WHERE customer.customer_id = remove_customer.customer_id;
total_customers := total_customers - 1;
END;

我想要的是仅在交货日期已过时删除该客户?所以在上面的过程中会有一个 if 语句我只是不确定如何在哪里以及如何添加它。

这将是沿着

CREATE PROCEDURE remove_customertest (customer_id VARCHAR2) IS
BEGIN
IF placed_order.delivery_date < SYSDATE
THEN
DELETE FROM order_line
WHERE  order_line.FK1_order_id in
(SELECT order_id FROM placed_order
 WHERE placed_order.FK1_customer_id = remove_customer.customer_id
);
DELETE FROM placed_order
WHERE placed_order.FK1_customer_id = remove_customer.customer_id;
DELETE FROM customer
WHERE customer.customer_id = remove_customer.customer_id;
ELSE
DBMS_OUTPUT.PUT_LINE ('Customer currently has a order been delivered and cant be removed.');
END IF;
END;

有没有人对此有任何建议,或者我是否在正确的路线上?

感谢您的帮助,我对 PL/SQL 还很陌生

4

1 回答 1

0

this condition you can add in the starting of the procedure as follow -

PROCEDURE Remove_Customer(Customer_Id VARCHAR2) IS
  l_Order_Date DATE;
BEGIN
  BEGIN
    SELECT MAX(Delivery_Date)
      INTO l_Order_Date
      FROM Placed_Order
     WHERE Placed_Order.Fk1_Customer_Id = Customer_Id;
  EXCEPTION
    WHEN OTHERS THEN
      l_Order_Date := SYSDATE - 1;
  END;
  IF l_Order_Date < SYSDATE THEN
    DELETE FROM Order_Line
     WHERE Order_Line.Fk1_Order_Id IN
           (SELECT Order_Id
              FROM Placed_Order
             WHERE Placed_Order.Fk1_Customer_Id = Customer_Id);
    DELETE FROM Placed_Order
     WHERE Placed_Order.Fk1_Customer_Id = Customer_Id;
    DELETE FROM Customer WHERE Customer.Customer_Id = Customer_Id;      
    --Total_Customers := Total_Customers - 1; -- is it a global variable in the package?
  ELSE
    Dbms_Output.Put_Line('Customer currently has a order been delivered');
  END IF;
END;
于 2013-05-09T10:38:45.187 回答