仅当数据库高于 sysdate 时删除的过程

Posted

技术标签:

【中文标题】仅当数据库高于 sysdate 时删除的过程【英文标题】:Procedure to delete only if database higher then sysdate 【发布时间】:2013-05-09 10:04:49 【问题描述】:

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

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 的新手

【问题讨论】:

【参考方案1】:

您可以在程序开始时添加此条件,如下所示 -

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;

【讨论】:

您好,感谢我尝试了代码,但它只是想出了“无效的 sql 语句”。我在我的问题中添加了一些代码,但它不会运行任何想法? 我的错..我只是在告诉位置和一些建议来实现这个逻辑..不执行代码.. 您在编辑后尝试过吗?这次我希望您在编译时不会出现任何错误.. 嗨,是的,谢谢,我已经寻求不同的解决方案,但这也很好。再次感谢

以上是关于仅当数据库高于 sysdate 时删除的过程的主要内容,如果未能解决你的问题,请参考以下文章

仅当我在代码中放置断点时,才能使用C#删除存储过程的参数

仅当音频高于特定声级时如何录制音频?

仅当表存在时如何删除 Amazon Redshift 中的表

仅当缩放高于某个值时才显示markerOptions 的优化而非弃用方式

仅当高于 0 时才计算每列的最小值和最大值之间的差异

php更新特定用户特定点