Oracle - Trigger is created, but gives error anytime data is updated - sql

I'm creating the following trigger:
CREATE TRIGGER Trigger_UpdateTrainingDelivery
AFTER DELETE OR INSERT OR UPDATE OF STARTDATE
ON TPM_TRAININGPLAN
BEGIN
UPDATE TPM_PROJECTVERSION V
SET TRAININGDELIVERYSTART = (SELECT MIN(STARTDATE) FROM TPM_TRAININGPLAN WHERE PROJECTID=V.PROJECTID AND VERSIONID=V.VERSIONID AND TRAININGPLANTYPE='prescribed')
END;
When I create it, I get a warning:
Warnings: --->
W (1): Warning: execution completed with warning
<---
However, it's still created it anyway. When I then modify a row in TPM_TRAININGPLAN, I get an error:
>[Error] Script lines: 12-12 ------------------------
ORA-04098: trigger 'TPMDBO.TRIGGER_UPDATETRAININGDELIVERY' is invalid and failed re-validation
Script line 12, statement line 1, column 7
Is there something wrong with my trigger? I can run the UPDATE statement in the trigger by itself and it runs fine, so I don't think there's anything wrong with that.

It appears that you are missing a semicolon at the end of your UPDATE statement.
If you query USER_ERRORS, you'll get the same error information that SQL*Plus will give you with the SHOW ERRORS command without needing to have access to SQL*Plus.
SELECT line, position, text
FROM user_errors
WHERE name = 'TRIGGER_UPDATETRAININGDELIVERY'
ORDER BY sequence

What was the error being reported during compilation?
SQL> show errors trigger trigger_updatetrainingdelivery

Ok I figured this out. It's actually a bug in Aqua Data Studio, which I'm using to run the query. For some reason, it doesn't handle semi-colons within the triggers correctly. I will report this bug, but I did find a workaround:
File->Options->General
Uncheck: ';' Statement separator

Related

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().

Informix SQL update command error 746

I tried to update the field "contract_id" in the table "contract_scan_image".
However, the update was failed and an error "746: Field contract_id and type of contract_scan_image cannot be updated!" was shown.
My SQL command is:
update contract_scan_image
set contract_id = '14864730'
where contract_id = '1486473'
and type = 'RM'
and account = '00193400944'
Does anyone know what happened and how to fix it?
Error message -746 is for user-defined errors. It typically is used in stored procedures in a RAISE EXCEPTION statement:
RAISE EXCEPTION -746, 0, "Field contract_id and type of contract_scan_image cannot be updated!"
The actual message text for error -746 in the message files is:
%s
That is, it prints the string it is given as a string.
So, you are going to need to track down the triggers and stored procedures invoked by those triggers on the contract_scan_image table, and deduce from where the error is generated what you are doing wrong. Superficially, though, it appears that you are not allowed to alter the contract ID, yet that is what you are trying to do.
First things first, I would take a look at a list of Reserved words in SQL - https://drupal.org/node/141051
I would get in the habit of surrounding fields with `` See below:
update contract_scan_image
set `contract_id` = '14864730'
where `contract_id` = '1486473'
and `type` = 'RM'
and `account` = '00193400944'
** Note - type is a reserved word
The error is caused by something being triggered. Then no table can be modified by UPDATE command.
Finally I deleted the record I want to update. Then added back the modified record.
I copy the error description here from the net for reference.
BTW, I asked my supervisor and he said he did trigger something to cause this. (He didn't tell me how to un-trigger it...)
-746
THE SQL STATEMENT IN FUNCTION, TRIGGER, OR IN STORED PROCEDURE name VIOLATES THE NESTING SQL RESTRICTION
Explanation
If a table is being modified (by INSERT, DELETE, UPDATE, or MERGE), the table can not be accessed by the lower level nesting SQL statement.
If any table is being accessed by a SELECT statement, no table can be modified (by INSERT, DELETE, UPDATE, or MERGE) in any lower level nesting SQL statement.
System action
The SELECT, INSERT, DELETE, UPDATE or MERGE SQL statement failed.
Programmer response
Remove the failing statement from the named function, trigger or the stored procedure.
SQLSTATE
57053

Oracle Warning: execution completed with warning

I have two tables
Orders(ID,ORDERDATE,DELIVERYDATE,GOODID,QUANTITY,COLLECTIONFROM,DELIVERYTO,NOTES)
and
ROLLINGSTOCK_ORDER(ORDERID,ROLLINGSTOCKID,DEPARTUREDATE,DELIVERYDATE,ROUTEID)
i have created a trigger to update the DELIVERYDATE in ROLLINGSTOCK_ORDER when DELIVERYDATE is updated in Orders
CREATE OR REPLACE TRIGGER TRIGGER_UpdateDeliveryDate
BEFORE UPDATE OF DELIVERYDATE ON Orders
FOR EACH ROW
BEGIN
then
UPDATE LOCOMOTIVE_DRIVER ld
set ld.DELIVERYDATE = :new.DELIVERYDATE
where ld.orderid = :new.id
end if;
END;
When i run it i get the following message
Warning: execution completed with
warning TRIGGER
TRIGGER_UpdateDeliveryDate Compiled.
The warning does not give me any information so
How can i see the details of the warning?
The trigger seems ok to me can you spot the problem?
Thanks
Earlier this week you asked a question on writing a trigger to execute a conditional update. I posted two examples of how to achieve that end. What you appear to have done is mash the two examples into a single spavined trigger which doesn't compile.
To be clear you need just one of the following. Either
BEFORE UPDATE OF DELIVERYDATE ON Orders
or
BEFORE UPDATE ON Orders
...
if :new.delivery_date != :old.delivery_date then
Use the first option if you just have the one piece of logic to apply. Use the second version if you want your trigger to handle other pieces of logic as well, which is usually the case.
You write
BEGIN
then
which is incorrect syntax. Are you missing an IF?
You are also missing a semicolon (;) after your UPDATE.
You might get the error using
Show Error Trigger TRIGGER_UpdateDeliveryDate

ORA:00900 Invalid sql statement

i created a procedure with 32 in argument,it sucessfully created.but when i am executing this in back end oracle the errror came ORA:00900 Invalid sql statement
Use:
SQL> alter procedure [your procedure name here] compile;
SQL> show errors
...to be able to diagnose the issue from the resulting error output.
Also look at view USER_ERRORS.
Sometimes, show errors does not show anything when in fact there are errors. Especially after alter compile.
Finally, re-compile in TOAD or SQL Developer and you can easily navigate to the error.
In Oracle SQL Developer you should execute it this way:
BEGIN
YOUR_PROCEDURE(PARAM1, PARAM2);
END;
If you use EXECUTE or EXEC (which work in SqlPlus) you get the ORA-00900 error.

Calling a Stored Procedure in Oracle

This should be something fairly simple and straight-forward but for some reason I can't get it to work. I've created my SProc like so:
create or replace procedure zadmit_migrate_data (zadmit_seq_no number)
is
thisPIDM number;
begin
select pidm into thisPIDM
from saturn.zadmit_core_data
where pk_seqno = zadmit_seq_no;
if thisPIDM is not null then
dbms_output.put_line('thisPIDM is NOT null!');
else
dbms_output.put_line('thisPIDM is null!');
end if;
end zadmit_migrate_data;
And now I've trying to call it like this:
call zadmit_migrate_data(4);
And then I get this error:
ORA-06575 Package or function is in an invalid state.
So I tried this:
execute zadmit_core_data(4);
And instead get this error:
ORA-00900 Invalid SQL statement.
It might be less time consuming to point out where I'm going right, but if anyone can tell me where I'm going wrong that would probably be more useful. :)
If you Google for error ORA-06575, you find:
You tried to execute an SQL statement
that referenced a PLSQL function that
is in an invalid state. This happens
when the function is compiled with
errors.
You can resolve this by:
Correct the errors and then re-compile
the function. Then re-execute the SQL
statement.
Once your procedure compiles, you could execute it like:
begin
zadmit_migrate_data(4);
end;
Shouldn't your execute statement be "execute zadmit_migrate_data(4);" ?
At any rate, running this command:
SELECT object_name FROM user_objects WHERE status='INVALID';
will tell you if you procedure is valid or not.
Executing your CREATE or REPLACE ... command, then immediately executing the command
SHOW ERRORS
should tell you what compilation errors were encountered.
Run this
SQL> alter procedure zadmit_migrate_data compile;
SQL> show errors
Given the simplicity of your procedure, it shouldn't be hard to diagnose the resulting error stack.