Procedure to delete only if database higher then sysdate

情到浓时终转凉″ 提交于 2019-12-25 05:46:04

问题


Ive currently a procedure that deletes a customer a fairly simple procedure that's inside a package body. Here is the code for the procedure that runs and deletes the customer:

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;

What i want is to only delete that customer if the delivery date has past? so there would be a if statement in the above procedure I'm just unsure how where and how to add it.

It would be along the lines of

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;

Does any one have any advice on this or if I'm on the right lines?

Thanks for the help I'm fairly new to PL/SQL


回答1:


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;


来源:https://stackoverflow.com/questions/16459211/procedure-to-delete-only-if-database-higher-then-sysdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!