SQL Server Triggers and Replication - sql-server

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

Related

SQL Server 2016 Create Trigger to start after New Table is finished updating

I have a table that is pushed to me from another SQL Server. The table is dropped after it is rotated to a "Current Day" table (Current Data is rotated to prev day before this).
Currently we have jobs that are running to do the "rotating" that are set at a specific time. I had originally created a trigger but clearly a trigger won't work (as I figured out from the comments) since the DDL operation wont continue its flow until after this trigger is complete... It also looks like this is just not possible since I don't have control over the group that is pushing the data to us.
Resolution : I went to the org that pushes the data and requested they add a step that inserts a record into a TableLog table and I am doing my trigger off of that insert instead.
CREATE TRIGGER InsertTest
ON [pace].[Table_Load_Log]
after insert
AS
if exists(select Table_name from inserted where inserted.Table_name = 'POE_Task_Details_SE_TEMP')
BEGIN
--drop table dbo.newtable
exec dbo.sp_start_job N'Make Pace Tables From Temp Table Push’
END
GO
There is no way to do this with a trigger. If you need to know when a CRUD operation on a table is complete, you would need to execute a command after the CRUD operation in the same process that launches it.

SQL Server - trigger AFTER INSERT table in schema

I have a stored procedure to run after a set of tables, all belonging to the same schema [DATA_Countries], is dropped and then re-inserted.
The operation is performed by another application, which drops and recreates the target table, over which I have no control.
Since the table is dropped and recreated each time, I can not use triggers on each target table.
Is there a way to get a trigger for each time a table is inserted into a specific schema, to return the name of such table and launch a parametrized stored procedure?
Thanks!
Yes, You can create DDL Triggers on SQL Server to track the DDL Changes. For example, If I want to track the changes in Stored Procedures on my Database AdventureWorks, I can create a trigger like this
CREATE TRIGGER td_ProcTrack
ON AdventureWorks
FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE
AS
BEGIN
<my code>
END
Refer this Article for more detailed examples

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.

Two triggers on one table 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

Resources