I need to alter a trigger in SQL Server. After I am doing, do I just execute the trigger similar to how I would do for a Stored Procedure?
ALTER TRIGGER
Yes, that is right, just use ALTER. If you right-click on your trigger in Object Explorer in SSMS and select Script Trigger as/ALTER To, you will see the ALTER statement created for your trigger.
ALTER TRIGGER triggerName
ON tableName
FOR INSERT -- or update & delete
AS
-- sql here
http://msdn.microsoft.com/en-us/library/ms176072.aspx
You don't "execute" a trigger. Triggers are "triggered" at certain points depending upon your definition of them.
For example an AFTER UPDATE trigger would run for all rows updated after you send an UPDATE command to the table on which the trigger is created.
Related
If we have an alter table statement within a stored procedure. Will it just affect that session or will it affect all sessions?
We use the alter table to disable the triggers that occur because we don't want them run when the stored procedure occurs but want the update triggers to run at all other times.
Cheers,
"Alter table" is an DDS operation - it will change the DB structure for all connections.
Here is a trick: create temp table with unique name like #no_triggers_for_[procname] at the beginning of your sp and check for it's existence within triggers.
I have a trigger in sql server that contains more then thousand lines of code like
CREATE TRIGGER [dbo].[xyz] ON [dbo].[abc]
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON;
--Here more then 100 field is there.
DECLARE #ErrMsg NVARCHAR(2000)
--
--
DECLARE #IsPublished BIT
DECLARE #ParentVersionedTemplateID UNIQUEIDENTIFIER
--
--
Now I need to change the datatype of one of the field of this trigger. One possible way is copy and paste the whole code and use ALTER command and change the existing datatype whatever I want.
But I want to ask is there any other way where without drop and recreate the trigger or alter the complete trigger code can I modify the parameters of that trigger?
There is no way in SQL Server to alter part of a Trigger. You need to rewriting the whole trigger (via ALTER TRIGGER). My best suggestion would to to use SSMS, right click on the Trigger in question and select Script Trigger as Alter. This would be the least amount of effort.
I need to delete a trigger in SQL Server. Seems like it should be simple enough, but because there is a thing called a "delete trigger", a trigger that is invoked upon deletion, it seems impossible to find resources on how to actually delete an already existing trigger.
DROP TRIGGER:
Removes one or more triggers from the current database...
You can remove a trigger by dropping it or by dropping the trigger table. When a table is dropped, all associated triggers are also dropped. When a trigger is dropped, information about the trigger is removed from the sysobjects and syscomments system tables.
Use DROP TRIGGER and CREATE TRIGGER to rename a trigger. Use ALTER TRIGGER to change the definition of a trigger...
To drop a trigger, this works:
DROP TRIGGER [trigger_name];
If you want to check weather trigger exist before a drop, then use:
SELECT * FROM [sys].[triggers] WHERE [name] = 'MyTrigger'
For more check out http://www.tsql.info/triggers/drop-trigger.php and https://stackoverflow.com/a/636470/2218697
I can Drop a Trigger with the Following Query
DROP TRIGGER [Trigger_Name]
(OR)
DROP TRIGGER Trigger_Update
Hope this Must helpfull...
For SQL Server 2016 and above
DROP TRIGGER IF EXISTS [dbo].[trg]
https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-trigger-transact-sql
DROP TRIGGER IF EXISTS [dbo].[trigger_name];
i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.
How do i do this in SQL server?
in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.
Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.
You can check for the existence of a specific Trigger like so.
IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN
--Add your Trigger create code here
END
I find this to be a more compact SQL Server equivalent to MySQL's DROP TRIGGER IF EXISTS syntax:
IF OBJECT_ID('XXXX', 'TR') IS NOT NULL
DROP TRIGGER XXXX
I'd use something like:
IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]
replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).
Alternatively, you could just use
ALTER TRIGGER dbo.xxx [etc]
Since version 2016 this syntax is also supported by Microsoft SQL Server:
DROP TRIGGER IF EXISTS trigger_name
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.