Oracle Sql trigger not working - sql

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.

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?

trying to use trigger

I'm trying to activate a trigger when a new employee is added. the trigger should store the username of the person that has done the insert and the time it happened. Using Oracle database.
So far my code for the trigger is:
CREATE OR REPLACE TRIGGER insert_2
AFTER INSERT ON employees
for each row
DECLARE
vUser varchar(50);
begin
select user into vUser from dual;
insert into audit_cheker1 (date_create, inserted_by)
values (sysdate(), vUser);
END;
/
The trigger works but after I try to insert a new record it doesn't work and tells me error in the trigger.
The error message is telling you your trigger is invalid, that is it contains syntax errors and cannot be compiled. So you need to fix the compilation errors.
There are several ways to find errors. You can run a query:
select * from user_errors
where type = 'TRIGGER'
and name = 'INSERT_2'
/
You could use the SQL*Plus command show errors after the CREATE TRIGGER statement.
Or, as it seems you're using SQL Developer, you could open the trigger in the Object Navigator. You'll see the tab has several panes, one of which is labelled Errors. Open that pane to see what's wrong.
Here is one for free: although sysdate is technically a function it is a special one. It never takes any parameters and calling it with brackets is wrong. Remove these brackets: sysdate().

Stored procedure to run a query based off timestamp in table

I'm trying to write a procedure where the query will only run if the table has been updated at a certain time.
There is actually a timestamp in the table so I just need to check something like
IF table.update = GETDATE() THEN …
I'm not even sure where to start here.
Can anyone point me in the direction of a place where I can learn about stuff like this, or show me which functions I need to use?
You need to start by looking at how to use Database JOBs.
Since no stored procedure can start by itself.
Inside the Job, you can define how often and when stored procedure run.
The code inside the job can be simple SQL.
OR
You can create an update Trigger on the table. And inside the trigger base on the time call the stored procedure.
Update:
Just saw your latest comment. I have a similar app in my company. We use a Database Job to send a batch of auotmated emails on specific time everyday.
You need a trigger, in the UPDATE method, and you can have it run when it is saved in a separate table.
The new table must have the primary key and the time stamp with the GETDATE () method.
To Implement the Trigger:
create trigger trTriggerTest on tableName after update
As
Begin
set nocount on;
INSERT INTO TimeRegister(Id, date, ...) VALUES (#Id, GetDate(), ...);
end
go
To register en new table:
INSERT INTO TimeRegister(Id, date, ...) VALUES (#Id, GetDate(), ...);
Documentation: Trigger

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

ORA-04098: Simple trigger is invalid. Why?

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;