我在oracle中创建触发器时遇到问题,我有一个表"stock"。我有字段"xxx","yyy"和"zzz"。我想创建一个触发器,它可以在插入或更新后立即填充此"股票"表上的另一个字段。我已经厌倦了这样的事情,但我不能。
create or replace
Trigger Connector_Stock_Trg
AFTER INSERT OR UPDATE OF xxx,yyy,zzz ON STOCK
For Each Row
Declare
L_Temp VARCHAR2(64);
BEGIN
L_Temp := Case
When Xxx Is Not Null Then Xxx
When yyy Is Not Null And zzz Is Not Null Then yyy||'_'||zzz
When Yyy Is Null And zzz Is Not Null Then zzz
WHEN zzz IS NULL AND yyy IS NOT NULL THEN yyy
END;
Update Stock
SET CONNECTOR=L_Temp;
END;
您不必STOCK
更新表,但您只需要为:NEW.CONNECTOR
分配一个表达式,如下所示。
CREATE OR REPLACE TRIGGER CONNECTOR_STOCK_TRG BEFORE
INSERT OR UPDATE OF XXX, YYY, ZZZ ON STOCK
FOR EACH ROW
--DECLARE
-- L_TEMP VARCHAR2(64);
BEGIN
:NEW.CONNECTOR := -- USE :NEW
CASE
WHEN :NEW.XXX IS NOT NULL THEN :NEW.XXX
WHEN :NEW.YYY IS NOT NULL AND :NEW.ZZZ IS NOT NULL THEN :NEW.YYY
|| '_'
|| :NEW.ZZZ
WHEN :NEW.YYY IS NULL AND :NEW.ZZZ IS NOT NULL THEN :NEW.ZZZ
WHEN :NEW.ZZZ IS NULL AND :NEW.YYY IS NOT NULL THEN :NEW.YYY
END;
-- UPDATE STOCK
-- SET
-- CONNECTOR = L_TEMP;
END;
/
注意:您可以使用 Oracle 中的Virtual Columns
概念来满足此类要求。
干杯!!