Как удалить триггер sql server
Перейти к содержимому

Как удалить триггер sql server

  • автор:

How to delete trigger in SQL Server?

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.

Brian Tompsett - 汤莱恩's user avatar

5 Answers 5

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.

SQL Server DROP TRIGGER

Summary: in this tutorial, you will learn how to use the SQL Server DROP TRIGGER statement to remove existing triggers.

Introduction SQL Server DROP TRIGGER statements

The SQL Server DROP TRIGGER statement drops one or more triggers from the database. The following illustrates the syntax of the DROP TRIGGER statement that removes DML triggers:

  • IF EXISTS conditionally removes the trigger only when it already exists.
  • schema_name is the name of the schema to which the DML trigger belongs.
  • trigger_name is the name of the trigger that you wish to remove.

If you want to remove multiple triggers at once, you need to separate triggers by commas.

To remove one or more DDL triggers, you use the following form of the DROP TRIGGER statement:

  • DATABASE indicates that the scope of the DDL trigger applies to the current database.
  • ALL SERVER indicates the scope of the DDL trigger applies to the current server.

To remove a LOGON event trigger, you use the following syntax:

Notice that when you drop a table, all triggers associated with the table are also removed automatically.

SQL Server DROP TRIGGER examples

A) SQL Server DROP TRIGGER – drop a DML trigger example

The following statement drops a DML trigger named sales.trg_member_insert :

B) SQL Server DROP TRIGGER – drop a DDL trigger example

The following statement removes the trg_index_changes trigger:

In this tutorial, you have learned how to use remove a trigger using the DROP TRIGGER statement.

How can I drop all triggers in a single database?

I have a database with 104 triggers, is there a way to delete all the triggers with a single command from a single database called ‘system_db_audits?

Mohamed Mahyoub's user avatar

4 Answers 4

You can use Dynamic SQL and the sys.triggers DMV to build query that you can execute.

is_ms_shipped excludes any triggers that were shipped with SQL Server.
parent_class_desc filters for object level triggers, rather than database level.

Change the PRINT to an EXEC once you are happy with the output.

Use Sys.Triggers Meta Data Table which contains a row for each object that is a trigger

  1. Change output mode to text by clicking the toolbar button shown here:

enter image description here

Execute this script:

Copy Output into a new SQL Server Management Studio window, verify the code performs the actions you are expecting, and Execute.

AA.SC's user avatar

In case you want to run a sql job on a central server [ServerA] to do the trigger deletion work, I’ll provide a PowerShell version assuming you have a SQL Server 2012 (or above) instance with SQLPS module installed on [ServerA]

Say you want to delete all triggers in [AdventureWorks] database on [ServerB] SQL Server instance (SQL Server 2005+).

You can run the following PS on [ServerA]:

Please remember to replace ServerB and AdventureWorks with your own values.

This is pretty flexible solution which you can easily customize to adapt to other different requirements, such as only delete triggers belong to a specific set of tables, or disable (instead of delete) some specific triggers etc.

Strictly speaking, the solutions provided by @Mark Sinkinson is not correct because the requirement is not to delete triggers on ‘system_db_audits’ db, but to delete triggers in another db from ‘system_db_audits’. This means you need to create a dynamic sql in ‘system_db_audits’ to wrap the «dynamic sql» provided by @Mark Sinkinson to delete those target triggers assuming that both ‘system_db_audits’ and the target db are on the same sql server instance. Otherwise if the two dbs are not on the same instance, it will be even much «ugly» to handle the deletion (such as via linked server etc). In such scenario, PS is an elegant solution no matter where the target db is or is not on the same sql instance.

Как удалить триггер sql server

We and our partners use cookies to Store and/or access information on a device. We and our partners use data for Personalised ads and content, ad and content measurement, audience insights and product development. An example of data being processed may be a unique identifier stored in a cookie. Some of our partners may process your data as a part of their legitimate business interest without asking for consent. To view the purposes they believe they have legitimate interest for, or to object to this data processing use the vendor list link below. The consent submitted will only be used for data processing originating from this website. If you would like to change your settings or withdraw consent at any time, the link to do so is in our privacy policy accessible from our home page..

Manage Settings Continue with Recommended Cookies

Modify or Delete Triggers in SQL Server

Here you will learn how to modify and delete triggers in SQL Server.

The ALTER TRIGGER statement is used to modify the definition of an existing trigger without altering the permissions or dependencies.

In the above ALTER TRIGGER statement:

  • trigger_name is the trigger you wish to alter.
  • ON specifies the table or view on which the trigger is created
  • FOR indicates when the trigger must fire when an event happens
  • [INSERT], [UPDATE], [DELETE] specifies the list of events that will cause the trigger to fire.

The following modifies a DDL trigger.

Delete a Trigger

DROP TRIGGER statement drops one or more triggers from the database. You can delete multiple triggers using the DROP TRIGGER statement by specifying the trigger names separated by a comma.

For example, the below SQL statement drops the DML trigger trgEmployeeUpdate .

Delete DDL or LOGON Triggers

Use the DROP TRIGGER ON statement to delete DDL or LOGON triggers.

For example, the below SQL statement drops the DDL trigger trgTablechanges using ON DATABASE option because it is a database level trigger.

The following drops the LOGON trigger trgLoginConnection using ON ALL SERVER option because it is the server level trigger.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *