SQL Trigger Fake Update - sql

I have a table and a trigger. Also I have a stored procedure to update the table. The Trigger is worked after the update. When I start the update the table by using stored procedure, trigger starts and there is an infinite loop. To prevent this infinite loop I disable the trigger at the beginning of the procedure and I enable the trigger at the end of the procedure.
I want to make a fake update to fire the trigger after enabling. Is it possible, if yes how can I make a fake update?

Related

Is it Possible to Disable the Triggers in the SQL Server 2012 for the Particular Stored Procedure Run While it will be active in the other Processes?

Is it possible to disable the triggers for a particular stored procedure run while it will be active in the other processes?
Like if
update Table A
having Update Trigger TrA with a SP
Trigger will not work but if I do the update manually will the trigger work?
Any property for the SP.

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

Are Sql Triggers synchronous or asynchronous?

I have a table that has an insert trigger on it. If I insert in 6000 records into this table in one insert statement from a stored procedure, will the stored procedure return before the insert trigger completes?
Just to make sure that I'm thinking correctly, the trigger should only be called (i know 'called' isn't the right word) once because there was only 1 insert statement, right?
My main question is: will the sproc finish even if the trigger hasn't completed?
Your insert trigger will run once for the entire insert statement. This is why it is important to use the inserted temporary table to see what has actually been inserted, and not just select the most recent single record, or something like that.
I just tested an insert and update trigger and indeed, they are considered part of the insert by sql server. the process will not finish until the trigger finishes.
Triggers are part of the transaction that called them.
One important thing about triggers that you must be aware of is that the trigger fires once for each transaction (at least in SQL server, you should check other dbs, but even if it will process row by row, that is usually a poor idea), so if you insert 6000 records the trigger fires once not 6000 times. Many people are not aware of this and write triggers as if they will process multiple record inserts one record at a time. This is not true and your trigger must account for handing the multiple record insert.
The trigger call is not asynchronous. Each call to your insert procedure will result in the trigger being fired, and the procedure will not return until the trigger finishes.
Take a look at the query plan to see how it works. You'll see that the statements in the trigger will be called for each call to the procedure.
The thing is, every time the TRIGGER criteria is met, the TRIGGER fires. It fires once in batch processing or Transaction. See my lesson 101 on TRIGGER
You should use cursor in insert statement to process trigger row.
Because Triggers in SQL Server fire once per statement, not once per row.