Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between database level trigger and server level trigger in SQL Server

Tags:

sql

sql-server

Can anyone please tell me the difference between database level trigger and server level trigger in SQL Server ?

Thanks in advance.

like image 596
leeg4625 Avatar asked Sep 25 '22 12:09

leeg4625


1 Answers

SQL Server 2005 introduced DML Triggers that can be set to fire on your chosen DDL events such as CREATE_TABLE, ALTER_TABLE, DROP_TABLE, ALTER_DATABASE, CREATE_LOGIN etc.

DDL Triggers can be set within 2 scopes:

  1. Server scope: Triggers created with Server scope must target server DDL events such as CREATE_DATABASE or CREATE_LOGIN
  2. Database scope: Triggers created with database scope must target database level events such as CREATE_TABLE or ALTER_PROC.

See the full list of SQL Server DDL Trigger Events (including their scope) on msdn here.

Syntax of a DDL trigger:

CREATE TRIGGER [TriggerName]
ON [Scope (Server|Database)]
FOR [EventName...],
AS
-- code for your trigger response here
like image 157
Deep Kalra Avatar answered Dec 31 '22 22:12

Deep Kalra