Two triggers on one table sql server - sql-server

I have two triggers on one of the tables in my database. One is for insert trigger and the other is an after update trigger. The insert trigger will update the table with values. My question is this; Is it possible that that the update trigger is firing at the same time that the insert is doing its job?

A FOR INSERT trigger will fire only on INSERT statements. A FOR UPDATE trigger will fire only on UPDATE statements. Of course, if your insert trigger executes any UPDATE statements then it will fire the update trigger, and vice versa.
Your UPDATE trigger won't fire for an INSERT statement (excepting the update-within-trigger case above), but of course you still have to design for concurrency, since it's possible for two different users to be running two different operations at the same time - one INSERT and one UPDATE.

If your insert trigger does an update to the table, the update trigger will be called. If a trigger triggers another trigger, it's called a "recursive trigger".
Recursive triggers can be disabled for an entire server:
sp_configure 'nested_triggers', 0
go
reconfigure
Or for one database:
alter database yourdb set recursive_triggers off

Related

Trigger doesn't fire when row is inserted

I built a trigger in SQL Server to execute a stored procedure when a new row is inserted into the table Balance Data, but the trigger doesn't get fired. I don't know what I am doing wrong or what is happening.
This is the script:
CREATE TRIGGER [dbo].[SP_Trigger]
ON [dbo].[BalanceData]
FOR INSERT
AS
BEGIN
SET NOCOUNT ON;
Exec Schenck.dbo.spCopyData
END
I assume that you are using Transact-SQL.
According to the documentation, FOR INSERT triggers are synonymous with AFTER INSERT triggers by default. This should fire after you have inserted your data into [dbo].[BalanceData].
I would firstly confirm that the data has been inserted successfully (i.e. no check constraint violations, etc) and then confirm what Schenck.dbo.spCopyData is doing. You have turned ROWCOUNT off in the trigger, so perhaps this has given you the illusion that nothing happened.

Insert/Update Triggers

I have 2 triggers (Insert and Update)
CREATE TRIGGER my_InsertTrigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
Update a previous record if found
END //
CREATE TRIGGER my_UpdateTrigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
.....
END //
According to my understanding triggers cannot be fire programmatically/manually, it only fires when an insert/update/delete happens on that table.
So my question is in the case mentioned above, will the insert trigger(my_InsertTrigger) invoke the update trigger (my_UpdateTrigger)?
Thanks in advance.
When I changed the trigger syntax to SQL Server (with just inserting one row) one trigger that causes an update on a table will kick off the update trigger on that table (if it exists).
I have seen this on audit tables and two things can, and will, happen:
1) Performance will suffer.
2) If the audit triggers start kicking each other off (by having cross trggers) you can run out of stack space (The triggers can nest 32 levels down).

SQL Server - For Insert trigger

With 'For Insert' trigger, are rows inserted to the table yet when it is triggered?
CREATE Trigger check_availability on Room
For Insert, Update
Thanks!
Yes.
CREATE TRIGGER
FOR | AFTER
AFTER specifies that the DML trigger is fired only when all operations specified in the triggering SQL statement have executed successfully. All referential cascade actions and constraint checks also must succeed before this trigger fires.
AFTER is the default when FOR is the only keyword specified.
AFTER triggers cannot be defined on views.

SQL Server Triggers and Replication

I have a trigger on update of a table that inserts into another table. This other table only exists in one location (the table is not replicated). If I set the trigger as not for replication, will my update trigger still fire when replicated data comes in to the table with the trigger? That way I can have the trigger and the tables that it inserts into in only one location.
No: this is the point of NOT FOR REPLICATION. From MSDN
Triggers
The trigger is not executed when a replication agent performs an insert, update, or delete operation.
The key here is "replication agent": this is the process that makes the trigger not fire

SQL Server: pause a trigger

I am working with SQL Server 2005 and I have trigger on a table that will copy an deletions into another table. I cannot remove this trigger completely. My problem is that we have now developed an archiving strategy for this table. I need a way of "pausing" a trigger when the stored proc that does the archiving runs.
A little more detail would be useful on how the procedure is accessing the data, but assuming you are just getting the data, then deleting it from the table and wish to disable the trigger for this process, you can do the following
DISABLE TRIGGER trg ON tbl;
then
ENABLE TRIGGER trg ON tbl;
for the duration of the procedure.
This only works for SQL 2005+
An alternative method is to use Context_Info to disable it for a single session, while allowing other sessions to continue to fire the trigger.
Context_Info is a variable which belongs to the session. Its value can be changed using SET Context_Info.
The trigger will mostly look like this:
USE AdventureWorks;
GO
-- creating the table in AdventureWorks database
IF OBJECT_ID('dbo.Table1') IS NOT NULL
DROP TABLE dbo.Table1
GO
CREATE TABLE dbo.Table1(ID INT)
GO
-- Creating a trigger
CREATE TRIGGER TR_Test ON dbo.Table1 FOR INSERT,UPDATE,DELETE
AS
DECLARE #Cinfo VARBINARY(128)
SELECT #Cinfo = Context_Info()
IF #Cinfo = 0x55555
RETURN
PRINT 'Trigger Executed'
-- Actual code goes here
-- For simplicity, I did not include any code
GO
If you want to prevent the trigger from being executed you can do the following:
SET Context_Info 0x55555
INSERT dbo.Table1 VALUES(100)
Before issuing the INSERT statement, the context info is set to a value. In the trigger, we are first checking if the value of context info is the same as the value declared. If yes, the trigger will simply return without executing its code, otherwise the trigger will fire.
source: http://www.mssqltips.com/tip.asp?tip=1591
if DISABLE TRIGGER/ENABLE TRIGGER is not an option for some reason, you can create a table with a single row which will serve as a flag for the trigger.

Resources