Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does a Server trigger save in SQL Server?

A couple of days ago , I was practicing and I wrote some triggers like these :

create trigger trg_preventionDrop
on all server 
for drop_database
as
print 'Can not Drop Database'
rollback tran

go 

create trigger trg_preventDeleteTable
on database
for drop_table
as 
print 'you can not delete any table'
rollback tran

But the problem is I don't know where it has saved and How can I delete or edit those.

Thank you

like image 718
Mostafa Avatar asked Oct 01 '10 21:10

Mostafa


People also ask

Where is trigger saved in SQL Server?

Server-scoped DDL triggers appear in the SQL Server Management Studio Object Explorer in the Triggers folder. This folder is located under the Server Objects folder. Database-scoped DDL triggers appear in the Database Triggers folder.

Where can I find SQL triggers?

Using SQL Server Management Studio Expand the database that you want, expand Tables, and then expand the table that contains the trigger for which you want to view the definition. Expand Triggers, right-click the trigger you want, and then click Modify. The definition of the DML trigger appears in the query window.

Are triggers stored in database?

Notice that triggers are stored in the database separately from their associated tables. Triggers can be defined only on tables, not on views. However, triggers on the base table(s) of a view are fired if an INSERT, UPDATE, or DELETE statement is issued against a view.

How do I save a trigger in SQL?

The code that is generated is "CREATE TRIGGER" code... when you put your trigger inside, you need to execute (F5) this entire query, which saves it to the database. Then click on the 'Triggers' folder for the table and hit F5 again to refresh, and you'll see your trigger. Save this answer.


2 Answers

Server Trigger

You can see them here

select * from sys.server_triggers

To drop use this syntax

drop trigger trg_preventionDrop on all server 

In Management Studio they are under the "Server Objects" -> "Triggers" node

Database Trigger

You can see them here

select * from yourdb.sys.triggers

To drop use this syntax

drop trigger trg_preventDeleteTable on database

In Management Studio they are under the "Databases" -> "yourdb" -> "Programmability" -> "Database Triggers" node

like image 146
Martin Smith Avatar answered Oct 05 '22 00:10

Martin Smith


trigger on a particular table resides in "DataBase" -> "YourDb" -> "YourTable" -> "Trigger" folder in Management Studio

also one can find the trigger on a particular table by execution the following sql:
EXEC sp_helptrigger yourtablename

like image 20
Amit Sinha Avatar answered Oct 04 '22 23:10

Amit Sinha