我在上一篇文章(https://stackoverflow.com/questions/16520216/ora-01843-not-a-valid-month)中问过这个问题,但从那以后它发生了变化,我认为这样做会更好从一开始就更清楚。
我在运行从客户表中删除客户的过程时收到此错误。
BEGIN
customers.remove_customer('17023');
END;
但是,当我从客户表中删除客户时,触发器会触发,这是我认为问题所在,这里是触发器的代码:
create or replace TRIGGER CUSTOMER_AD
BEFORE DELETE ON CUSTOMER
REFERENCING OLD AS OLD
FOR EACH ROW
DECLARE
pragma autonomous_transaction;
nPlaced_order_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO nPlaced_order_count
FROM PLACED_ORDER p
WHERE p.FK1_CUSTOMER_ID = :OLD.CUSTOMER_ID;
IF nPlaced_order_count > 0 THEN
INSERT into previous_customer
(customer_id,
first_name,
last_name,
address)
VALUES
(:old.customer_id,
:old.first_name,
:old.last_name,
:old.address);
END IF;
END CUSTOMER_AD;
现在,当我删除一个在placed_order 表中没有记录的客户时,该记录被删除了,当尝试删除一个在placed_order 表中有记录的客户时会出现错误消息。因此,我认为错误发生在触发器中,因为触发器仅将值插入到 previous_customer 表中,前提是它们在placed_order 表中有记录。但我仍然不明白为什么我会收到错误消息?
客户表上没有其他触发器。已下订单表上没有其他触发器,previous_employee 也没有
客户表的结构是:
"CUSTOMER_ID" VARCHAR2(40) NOT NULL ENABLE,
"FIRST_NAME" VARCHAR2(30),
"LAST_NAME" VARCHAR2(30),
"ADDRESS" VARCHAR2(30)
CONSTRAINT "PK_CUSTOMER" PRIMARY KEY ("CUSTOMER_ID") ENABLE
放置订单的结构是:
"ORDER_ID" NUMBER(*,0) NOT NULL ENABLE,
"ORDER_DATE" VARCHAR2(15),
"DELIVERY_DATE" VARCHAR2(15),
"FK1_CUSTOMER_ID" VARCHAR2(40) NOT NULL ENABLE,
CONSTRAINT "PK_PLACED_ORDER" PRIMARY KEY ("ORDER_ID") ENABLE
ALTER TABLE "PLACED_ORDER" ADD CONSTRAINT "FK1_PLACED_ORDER_TO_CUSTOMER" FOREIGN KEY ("FK1_CUSTOMER_ID")
REFERENCES "CUSTOMER" ("CUSTOMER_ID") ON DELETE CASCADE ENABLE
previous_customer 的结构是:
"CUSTOMER_ID" VARCHAR2(40),
"FIRST_NAME" VARCHAR2(30),
"LAST_NAME" VARCHAR2(30),
"ADDRESS" VARCHAR2(30)
有人知道为什么我会收到错误消息 ORA-01843 吗???
程序代码为:
PROCEDURE remove_customer (customer_id VARCHAR2) IS
ordersCount pls_integer;
BEGIN
select count(*) into ordersCount
from placed_order
where fk1_customer_id = remove_customer.customer_id
and delivery_date > sysdate;
IF ordersCount > 0
THEN
DBMS_OUTPUT.PUT_LINE ('Customer currently has a order been delivered and cant be deleted');
ELSE
DELETE FROM customer
WHERE customer.customer_id = remove_customer.customer_id;
total_customers := total_customers - 1;
END IF;
END;