ORA-04098: Simple trigger is invalid. Why? - sql

There is something wrong with this trigger. But what?
CREATE TRIGGER MYCOOLTRIGGER
AFTER INSERT ON MYCOOLTABLE
REFERENCING NEW AS newRow
FOR EACH ROW
DECLARE
BEGIN
END MYCOOLTRIGGER;
SQL Developer output:
Warning: execution completed with warning
TRIGGER MYCOOLTRIGGER Compiled.
Is there any way to get more info on this Warning?
P.S.
This question could use a better title. ;)

Oracle requires that you have something between BEGIN and END.
You can use NULL (a no-op):
CREATE OR REPLACE TRIGGER MYCOOLTRIGGER
AFTER INSERT ON MYCOOLTABLE
REFERENCING NEW AS newRow
FOR EACH ROW
DECLARE
BEGIN
NULL;
END MYCOOLTRIGGER;

If you want to see what the errors are:
show errors trigger mycooltrigger;

Related

need help writing this compound trigger

i have a column in my table that i need to update on insert or update, i can't do it with a simple trigger because the new value has to be SELECTED from the same table and other tables (which would generate trigger is mutating error).
my solution was to use a compound trigger, after a little bit of documentation i wrote this code:
CREATE OR REPLACE TRIGGER update_contract_n FOR
INSERT OR UPDATE ON contract
COMPOUND TRIGGER
v_n VARCHAR(70);
AFTER EACH ROW IS BEGIN
v_n := object_contract(:new.id_contract);
END AFTER EACH ROW;
AFTER STATEMENT IS BEGIN
UPDATE contract
SET
n = v_n
WHERE
id_contract = :new.id_contract;
END AFTER STATEMENT;
END update_contract_n;
the function object_contract() joins 4 tables one of which is the table contract and returns a varchar.
when i compile the trigger code i get this error :
pls-00679 trigger binds not allowed in before/after statement section
but i don't know what i'm doing wrong since the variable binding was done in AFTER EACH ROW section not in AFTER STATEMENT.
any help would be appreciated (fixing or rewriting the code), a simple guide on how to use a compound trigger would also be appreciated.
EDIT:
i found the problem, i was referencing :new.id_contract in AFTER STATEMENT and i fixed it, but now i am getting table is mutating trigger/function may not see it
how can i fix that?

Oracle Sql trigger not working

I have a very simple trigger, that prints out the username and date when a new row is inserted into the users table. But after it successfully compiled, the trigger didn't get triggered (there was no output in the dbms window). So, I simplified my code until I got this:
CREATE OR REPLACE TRIGGER logger
AFTER INSERT ON USERS
BEGIN
DBMS_OUTPUT.PUT_LINE('User added with name:');
END;
If I run the code in the SQL worksheet (from BEGIN to END), I can see the output, but not when I try to use the trigger. Where is the problem?
There are two options, one is that the trigger is not firing, the other is that it is, but you're not seeing the output.
Assuming you're working in a console program like sqlplus, make sure to do SET SERVEROUTPUT ON before inserting your row.
If that doesn't fix it, then make sure the trigger is firing. Try creating a simple table:
CREATE TABLE logtable ( msg VARCHAR2(30 CHAR));
Next, add this to your trigger,
INSERT INTO logtable( msg ) VALUES ( 'trigger fired' );
Then, try your insert again.

prevent oracle before update trigger failure to stop updating table

if you create before update trigger on a table and this trigger failed due to any reason the user can not update the table until you fix or remove the trigger.so is there any way to prevent this? i mean if you made a mistake on writing the trigger and wanted the update to continue
If your problem is that you are potentially creating an invalid trigger, then you should create the trigger in a disabled state. Then you can confirm that it compiles correctly before enabling it.
It doesn't help with runtime errors.
If you are replacing an existing trigger, the new code will still replace the old and will still be disabled. If you are relying on the trigger code to set a mandatory value then that will result in a runtime error.
CREATE OR REPLACE TRIGGER TEST_TRIG
BEFORE INSERT ON TEST
FOR EACH ROW
DISABLED
BEGIN
:NEW.TEST_KEY := TEST_SEQ.NEXTVAL;
END;

A Trigger which will implement update cascade to PK

I need to write a trigger which will implement update cascade for (gameno) code for SummerGames but I keep encountering `Error(11,1): PLS-00103: Encountered the symbol "CREATE
I'm using Oracle SQL Developer.
My current code:
create or replace TRIGGER SG_GAMENO_UPDATE
BEFORE UPDATE OF SG_GAMENO ON SUMMERGAMES
FOR EACH ROW
BEGIN
UPDATE SUMMERGAMES
SET SG_GAMENO = :new.SG_GAMENO
WHERE SG_GAMENO = :old.SG_GAMENO;
END;
You need to end the trigger statement, which is partly PL/SQL, with a / on a line on its own. That will end the statement and cause it to be run.
create or replace TRIGGER SG_GAMENO_UPDATE
BEFORE UPDATE OF SG_GAMENO ON SUMMERGAMES
FOR EACH ROW
BEGIN
UPDATE SUMMERGAMES
SET SG_GAMENO = :new.SG_GAMENO
WHERE SG_GAMENO = :old.SG_GAMENO;
END;
/
At the moment whatever is folowing this in your script - which seems to be another create statement - is being seen as part of the same trigger creation, and that keyword isn't valid within a block.
Your trigger doesn't seem to make any sense, and at best will lead to infinite recursion when you attempt an update (which will be detected and killed), but that's a separate issue. Perhaps you meant to update a child table, rather than the same table the trigger is against. But you shouldn't really be updating a PK at all; that's why synthetic keys are generally preferred over natural ones. gameno sounds synthetic anyway.
it will something about the separation of your codes, i mean when you executed the code snippet above, you marked off some part from another code, that's why you get syntactic error. try to execute just the code above, delete anyting else from the sql editor

PostgreSQL trigger function executing multiple times

i am experimenting with triggers in postgreSQL, but the trigger insert i would like to make is being done twice for some reason(THIS IS USING THE FOR EACH ROW), when i changed it to FOR EACH STATEMENT it was executing the insert 3 times. this is my sql script
CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$
BEGIN
insert into Audit values('k',124,'l');
return null;
END;
$tree_stamp$
LANGUAGE plpgsql;
create trigger forest_aud_ins after insert on forest
for each row execute procedure forest_aud_func()
insert into forest values('Blue',1600,'Austria','Health Ltd')
any idea why this is happening?
Thanks
i found out the problem, i was always creating new triggers but not deleting the previous ones, so each time i do an insert it was firing all the triggers i had done,
sorry and thanks for your help