Oracle trigger failed -ORA-04098 - sql

I have a table for which i have written a trigger:
CREATE OR REPLACE TRIGGER ac01_control_trigg
AFTER INSERT ON ac1_control_test
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.cur_pgm_name = 'LSN'
AND :NEW.nxt_pgm_name ='MD'
AND :NEW.file_status='RD' THEN
INSERT INTO ac1_control_test
(FILE_NAME, FILE_PATH,FILE_STATUS,CUR_PGM_NAME,NXT_PGM_NAME)
VALUES
(:NEW.FILE_NAME, :NEW.FILE_PATH,:NEW.FILE_STATUS,:NEW.CUR_PGM_NAME,'MD_MPS');
END IF;
END ac01_control_trigg;
when i am trying to insert into the table i am getting an error below!
ORA-04098: trigger 'CNGDB18.AC01_CONTROL_TRIGG' is invalid and failed re-validation
could anybody please help?
also when i compile the trigger in Toad,i am getting compile errors as below:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/65 PLS-00049: bad bind variable 'NEW_FILE_STATUS'
but what is the wrong with this?
and what does this error mean?

EDIT: Now that we see the message, the solution is easy :)
Use :NEW.file_status='RD' instead of:new_file_status='RD'
Your trigger object is invalid (there is a problem with the code).
Test this with:
SELECT object_name, status
FROM user_objects
WHERE object_name = 'AC1_CONTROL_TRIGG';
Should return:AC1_CONTROL_TRIGG INVALID
You can try the following in SQL*Plus to get a description of the error:
ALTER TRIGGER ac1_control_trigg COMPILE;
SHOW ERROR TRIGGER ac1_control_trigg;
Using TOAD, you can just type these two lines into an editor, select them and use Editor>Execute SQL via SQL*Plus.

Related

Subprogram or cursor 'USER' reference is out of scope

I'm trying to define a trigger which fires before any insert in Cliente table but I'm absolutely unable to do so. Here it is:
CREATE OR REPLACE TRIGGER updateSaldoCuenta
BEFORE INSERT ON Cliente
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20000, 'TRY');
END;
/
And here's the error I'm getting:
BEFORE INSERT ON Cliente
*
ERROR at line 2:
ORA-06552: PL/SQL: Compilation unit analysis terminated
ORA-06553: PLS-225: subprogram or cursor 'USER' reference is out of scope
Any help is appreciated!
Just creating a new user did the trick! I suppose something went wrong with the other

SQLite trigger error : rollback

I get the following error when I try to create this trigger in SQLite
create trigger timeslot_check1 after insert on section
for each row
when(new.time_slot_id not in(select time_slot_id
from time_slot))
begin
rollback
end;
ERROR : near "rollback": syntax error:
As shown in the documentation, the only SQL commands allowed in a trigger body are UPDATE, INSERT, DELETE, and SELECT.
To raise an error, you must use the RAISE() function from inside a query:
CREATE TRIGGER timeslot_check1
BEFORE INSERT ON section
FOR EACH ROW
WHEN NEW.time_slot_id NOT IN (SELECT time_slot_id FROM time_slot)
BEGIN
SELECT RAISE(FAIL, "invalid timeslot");
END;
Anyway, this check can be done much easier with a foreign key.

Can't fix Error: PL/SQL Statement ignored

I've just started working with Triggers. While trying to compile the next code fragment errors are listed in the log, like so: "Error(2,3): PL/SQL: SQL Statement ignored" and "Error(2,7): PL/SQL: ORA-00922: missing or invalid option".
I don't understand the problem. Can anybody help?
CREATE OR REPLACE TRIGGER CONT_VIG
BEFORE INSERT ON CONTRACTS
FOR EACH ROW
WHEN (OLD.CLIENTID = NEW.CLIENTID AND OLD.ENDDATE > NEW.STARTDATE)
BEGIN
SET (OLD.enddate = :NEW.startdate-1);
END;
look this this-oracle and this-stackoverflow
try this:
CREATE TRIGGER hr.salary_check
BEFORE INSERT OR UPDATE OF salary, job_id ON hr.employees
FOR EACH ROW
WHEN (new.job_id <> 'AD_VP')
pl/sql_block
You can't change OLD value...
replace
SET (OLD.enddate = :NEW.startdate-1);
with for example :
:NEW.startdate := sysdate ;
(I removed SET because I don't see the usefulness)

Oracle Trigger ORA-04098: trigger is invalid and failed re-validation

I am trying to create a simple trigger in an oracle 10g database. This script to Create the trigger runs clean.
CREATE OR REPLACE TRIGGER newAlert
AFTER INSERT OR UPDATE ON Alerts
BEGIN
INSERT INTO Users (userID, firstName, lastName, password) VALUES ('how', 'im', 'testing', 'this trigger')
END;
/
But when I run:
INSERT INTO Alerts(observationID, dateSent, message, dateViewed) VALUES (3, CURRENT_TIMESTAMP, 'Alert: You have exceeded the Max Threshold', NULL);
to activate the trigger, I get this error message:
ORA-04098: trigger 'JMD.NEWALERT' is invalid and failed re-validation
(0 rows affected)
I don't understand whats causes this error. Do you know what causes this error? Or why this is happening?
Thank you in advance!
-David
Oracle will try to recompile invalid objects as they are referred to. Here the trigger is invalid, and every time you try to insert a row it will try to recompile the trigger, and fail, which leads to the ORA-04098 error.
You can select * from user_errors where type = 'TRIGGER' and name = 'NEWALERT' to see what error(s) the trigger actually gets and why it won't compile. In this case it appears you're missing a semicolon at the end of the insert line:
INSERT INTO Users (userID, firstName, lastName, password)
VALUES ('how', 'im', 'testing', 'this trigger')
So make it:
CREATE OR REPLACE TRIGGER newAlert
AFTER INSERT OR UPDATE ON Alerts
BEGIN
INSERT INTO Users (userID, firstName, lastName, password)
VALUES ('how', 'im', 'testing', 'this trigger');
END;
/
If you get a compilation warning when you do that you can do show errors if you're in SQL*Plus or SQL Developer, or query user_errors again.
Of course, this assumes your Users tables does have those column names, and they are all varchar2... but presumably you'll be doing something more interesting with the trigger really.
Cause: A trigger was attempted to be retrieved for execution and was found to be invalid. This also means that compilation/authorization failed for the trigger.
Action: Options are to resolve the compilation/authorization errors, disable the trigger, or drop the trigger.
Syntax
ALTER TRIGGER trigger_Name DISABLE;
ALTER TRIGGER trigger_Name ENABLE;
in my case, this error is raised due to sequence was not created..
CREATE SEQUENCE J.SOME_SEQ MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE ;

Oracle Trigger create with compilation errors

I wrote the below trigger to prevent users from allocating a Class to a Session if the Class Date does not match the day of the week the Session is on.
CREATE OR REPLACE TRIGGER trig_alternative_classDate
AFTER INSERT OR UPDATE ON ALTERNATIVE
FOR EACH ROW
DECLARE
classdate CHAR;
sessionday VARCHAR;
BEGIN
SELECT to_char(to_date(class.class_date), 'Day') INTO classdate, sessions.day INTO sessionday
FROM SESSIONS, CLASS, DUAL, LOCATION, ALTERNATIVE
WHERE class.class_id = alternative.class_id
AND alternative.location_id = location.location_id
AND sessions.location_id = location.location_id;
IF sessions.day != to_char(to_date(class.class_date), 'Day')
THEN raise_application_error(-20999,'Invalid Class Date - Class Date does not match Session Day');
END IF;
END;
/
However I get an error message when I run the trigger
Warning: Trigger created with compilation errors.
SQL> show error trigger trig_alternative_classDate
Errors for TRIGGER TRIG_ALTERNATIVE_CLASSDATE:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/2 PL/SQL: SQL Statement ignored
5/80 PL/SQL: ORA-00923: FROM keyword not found where expected
Could someone please help?
Remove second INTO - only one INTO is needed:
INTO classdate, sessions.day INTO sessionday