tags:

views:

35

answers:

1

I have this trigger

procedure CASE_A  
as  
UPDATE table1 SET field1 = (SELECT bus FROM view1 WHERE table1.document = view1.document)  
end;

the below trigger is supposed to execute the above procedure CASE_A (Case_A is supposed to put a value into field1)

CREATE OR REPLACE TRIGGER "CASE_A" AFTER INSERT ON "TABLE1" FOR EACH ROW  
BEGIN  
CASE_A;
END;

but, it's not doing that.

A: 

you are doing it wrong, at least assuming this is Oracle.

better do it like this:

CREATE OR REPLACE TRIGGER "CASE_A" BEFORE INSERT ON "TABLE1" FOR EACH ROW  
BEGIN  
    SELECT bus INTO :new.field1 FROM view1 WHERE :new.document = view1.document;
END;

EDIT: since view1 depends on table1, we need to do a bit more... (read comments for more information)

CREATE OR REPLACE TRIGGER "CASE_A" BEFORE INSERT ON "TABLE1" FOR EACH ROW  
BEGIN  
    SELECT sum(FUNCTION_CASEA(b.DUE, b.RETD)) INTO :new.field1
      FROM table2
      WHERE table1.document = table2.document 
        AND table1.field1 = table2.branch;
END;
ammoQ
Not enough hating on Oracle here... aren't you a member of the Oracle haters club on DailyWTF?
R. Bemrose
R. Bemrose: Technically, yes. BTW, I'm a forum admin at TDWTF.
ammoQ
i am able to compile your coding. Because of this change, i am not able to save after modifying fields on the application in the same table.
unknown (google): which error message do you get? If view1 depends on table1, there is no easy way to solve it (mutating table problem...)
ammoQ
sorry. that was my mistake. the issue i had was with another trigger. that is fixed.coming back to CASE_A trigger, i don't get any error message. I can compile. But on the application, the value does not come up. What triggers this trigger? I do have a check mark on 'Trigger for Each Row'. If i don't have that, i get the following error message, 'ORA-04082: NEW or OLD references not allowed in table level triggers.'
currently, it's triggered for every INSERT on TABLE1. change it to ... BEFORE INSERT _OR UPDATE_ ON "TABLE1" FOR EACH ROW ... if you need it for updates, too. Without "For each row" it would only be fired once per statement; but one statement can affect many rows.
ammoQ
the moment i do that, i can't save any changes on the application. View1 does need to consider values from Table1<blink> VIEW CASE_A (DOC, BUS ) ASSELECT a.doc, SUM(FUNCTION_CASEA(b.DUE, b.RETD)) AS BUSfrom TABLE1 a, TABLE2 bwhere a.doc = b.doc and(a.field1=b.branch)group by a.doc</blink>That is my view.
Bad news: View1 does indeed depend on TABLE1. Good news: You do not really need it. I'll update my answer, because otherwise the code is not readable...
ammoQ
wow. i don't need a view and i don't need the procedure. You are genius. Thank you so much. I've been at this for a week now.