所以我试图构建一个触发器来维护两个表之间的数据一致性。我理想的触发器看起来应该是这样的(它可能不正确,我还没能正确调试它):
CREATE OR REPLACE TRIGGER salesFragmentGreaterThan
AFTER INSERT OR UPDATE OR DELETE ON sales
FOR EACH ROW WHEN (sale_price >= 5000000)
DECLARE
BEGIN
IF INSERTING THEN
INSERT INTO greaterThanFiveMillionFragment
VALUES (sales_seq.curval, :NEW.sales_date, :NEW.sale_type, :NEW.reserved_price, :NEW.sale_price, :NEW.deposit,
:NEW.balance, :NEW.buyer_id, :NEW.property_id);
ELSEIF DELETING THEN
DELETE FROM greaterThanFiveMillionFragment
WHERE greaterThanFiveMillionFragment.sales_id = :new.sales_id;
ELSEIF UPDATING THEN
UPDATE greaterThanFiveMillionFragment
SET :old.sales_date = :new.sales_date, :old.sale_type = :new.sale_type, :old.reserved_price = :new.reserved_price, :old.sale_price = :new.sale_price,
:old.deposit = :new.deposit, :old.balance = :new.balance, :old.buyer_id = :new.buyer_id, :old.property_id = :new.property_id
WHERE :old.sales_id = :newsales_id;
END IF;
/
这会抛出一系列无效的sql语句错误。我开始删除代码,找出导致这个问题的部分,直到我做到这一点:
CREATE OR REPLACE TRIGGER salesFragmentGreaterThan
AFTER INSERT OR UPDATE OR DELETE ON sales
FOR EACH ROW
WHEN (new.sale_price >= 5000000)
DECLARE
BEGIN
select sales_id from sales;
END;
/
这会抛出无效的sql错误。 我可以获得编译触发器的唯一方法是在BEGIN - END块内部没有任何内容。
我做错了什么?
此问题已标记为重复,我已被定向到此处: Declaring a variable and setting its value from a SELECT query in Oracle
恐怕我不明白这个问题是如何与我的相关的。我遇到的问题是,如果没有编译器将其声明为无效的SQL语句,我似乎无法在我的BEGIN块中放入任何代码。