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.
Related
Something peculiar is happening with one of my triggers. I have built a trigger which will check if the user is attempting to UPDATE or DELETE a row in the table which has [Released_Flag] = 'Y', and rollback the transaction if that is the case. However, if the user is attempting to update the Released_Flag field from 'N' to 'Y' for a row, it still returns the error.
My trigger is:
CREATE TRIGGER [dbo].[Prevent_Delete] ON [dbo].[Data_Test]
AFTER DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON
DECLARE #Release_Flag char(1)
SELECT #Release_Flag = [Release_Flag] FROM deleted
IF #Release_Flag='Y'
BEGIN
RAISERROR('Cannot update or delete a RELEASED variable.',16,1)
ROLLBACK
END
END
The above is causing some issues in UPDATE statements. It is sometimes raising the error, even if the row being updated has Release_Flag = 'N'. Any idea why this might be happening?
I also used SELECT * FROM [deleted] within the trigger to see what is happening, and some rows are showing Release_Flag = 'Y' in the [deleted] table, despite them actually being 'N'.
The issue with this that I did not realise is I have another trigger on the table which UPDATES the table. And so the old trigger is firing this new trigger, something I hadn't accounted for.
I have now moved the AFTER UPDATE section of this new trigger into my old trigger.
I have two tables in SQL Server. I want to raise an event when client inserts any row in order to copy that row to another table with more columns.
I have to do that in SQL Server just after insert. Is this possible? And if it is... how?
I need something like this:
CREATE EVENT myevent
ON (INSERT ROW?)
DO
TODO...
But I don't know if there are any event on insert and I donĀ“t know where does the code go.
Thanks.
EDIT:
I have another problem added. The tables are in different databases. I'm trying to implement this trigger:
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [TRIGGER_NAME]
ON [dbo].[TABLE_TRIGGERED]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO DB2.[dbo].[FINAL_TABLE]
SELECT *
FROM INSERTED
WHERE COL1= 'Stuff' AND COL2= 'Stuff' AND COL3=
(
SELECT MAX(COL3)+1
FROM DB2[dbo].[FINAL_TABLE]
)
END
And it can't access to COL1, COL2 and COL3. Is my sintax right?
Using an SQL After Insert trigger, you can handle it
If this is your first time with triggers, take care of set based coding.
If you miss it, on INSERT statements with multiple rows you might get unexpected results. So keep away using variables in the trigger code, think always set based using the Inserted and Deleted tables special to triggers
You can check the referred tutorial
I want to modify an existing trigger but I get an error.
After modifying this AFTER DELETE trigger when I delete a record from that table I get
The definition of object 'del_nota' has changed since it was compiled.
I've tried all the following
restart SQL Server
EXEC sp_recompile 'dbo.del_nota'
ALTER TABLE note DISABLE/ENABLE TRIGGER del_nota
But I get the same error
On that table I also have 2 triggers for Insert and for Update.
I've disabled them but still the same error.
That trigger initially deletes some records from another table.
I've added after that an insert statement in other table with a select the deleted record from the Deleted table.
What should I do ?
Thank you !
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
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.