prevent oracle before update trigger failure to stop updating table - sql

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;

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

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

Basic SQL trigger will not fire

This is my very first trigger and I'm not sure I have it set up correctly. I have a table called PayTypes. Here is my trigger syntax:
ALTER trigger payTypes_trigger
on PayTypes
AFTER INSERT, UPDATE, DELETE
as
PRINT 'AFTER TRIGGER EXECUTED SUCESSFULLY'
I run this with a breakpoint on the first line, update my VB.NET datagridview (which updates and saves just fine), but the breakpoint never gets hit.
Am I going about the setup of the trigger incorrectly?
There are two possible issues:
1) You are using VS Express, which does not support this behavior
2) You are not stepping into the trigger from a stored procedure.
I haven't tested this myself, but according to this MSDN documentation, you need to step into a stored procedure that will cause the trigger to fire in order to debug the trigger.

Solving the mutating table problem in Oracle SQL produces a deadlock

Hey, I'm trying to create a trigger in my Oracle database that changes all other records except the one that has just been changed and launched the trigger to 0. Because I am updating records in the same table as the one that launched the trigger I got the mutating table error. To solve this, I put the code as an anonymous transaction, however this causes a deadlock.
Trigger code:
CREATE OR REPLACE TRIGGER check_thumbnail AFTER INSERT OR UPDATE OF thumbnail ON photograph
FOR EACH ROW
BEGIN
IF :new.thumbnail = 1 THEN
check_thumbnail_set_others(:new.url);
END IF;
END;
Procedure code:
CREATE OR REPLACE PROCEDURE check_thumbnail_set_others(p_url IN VARCHAR2)
IS PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE photograph SET thumbnail = 0 WHERE url <> p_url;
COMMIT;
END;
I assume I'm causing a deadlock because the trigger is launching itself within itself. Any ideas?
Using an autonomous transaction for this sort of thing is almost certainly a mistake. What happens if the transaction that inserted the new thumbnail needs to rollback? You've already committed the change to the other rows in the table.
If you want the data to be transactionally consistent, you would need multiple triggers and some way of storing state. The simplest option would be to create a package with a collection of thumbnail.url%type then create three triggers on the table. A before statement trigger would clear out the collection. A row-level trigger would insert the :new.url value into the collection. An after statement trigger would then read the values from the collection and call the check_thumbnail_set_others procedure (which would not be an autonomous transaction).

Trigger not executing when a Batch Update runs

I am using VB.Net 2008 and ADO.Net to do a Batch Update to our Oracle database.
The updates are working, but there is a trigger on the table before the row is updated to enforce a member's termination termination date.
So if I was trying to set the termination date (via the batch update) to 31-Jan-2010 but the member had a claim that was processed on 2-Feb-2010 the trigger would force the termination date to be 2-Feb-2010. However, the trigger is NOT executing when the batch update runs?
Is there any Oracle DB Admin option that would disable Triggers on Batch Update?
A direct path load through SQL*Loader can disable and reenable triggers as described here
What does the batch update do. Maybe if it does a DELETE+INSERT, rather than an UPDATE, then the trigger won't fire.
The alter trigger command can be used to disable a trigger,
ALTER TRIGGER trigger_name DISABLE
This will show the enabled/disabled status of all triggers,
select trigger_name, status from user_triggers