What I was really looking for was how to edit an existing trigger in a database. In SQL 2000, you right click on a table on the Object Explorer details > All Tasks > Manage Triggers
In SQL 2005, you CANNOT right click on a table in the Object Explorer Details. You have to use the left side bar
And a short tutorial answering my questions. (Thanks Imran!)
This feature is also available in SQL Server 2005.
If you login into SSMS, expand Servername- expand database – expand table- under tables 4th subobject you will see trigger – expand triggers ( to see all triggers in that table) – right click on any one of the trigger and click modify and you will see a new query window with SQL code for that trigger, If you want to make changes to this trigger, make necessary changes in the sql code and then hot execute button at the top. ( same as to execute any query.) and you are done.
You also mentioned something about, Programmability under Database ( in object explorer, SSMS), let me explain you in brief what exactly is this.
Objects in a database can be : Tables, views, stored procedures and functions ….
So when we are talking about triggers, these are specific for tables, because triggers (DML, Data Manipulation Language Triggers) can be created on tables for/instead of insert ,update and delete. That is why you will see triggers under tables but not under Programmability.
Now the second question, When you expand programmability, you will see database triggers , what are these triggers ???
This is a brand new concept in SQL Server 2005, these are called DDL Triggers, Data Definition Language ( these are not available in SQL Server 2000).
These triggers, as the name defines are used for all DDL operations like create, alter, drop… You cant imagine how many types of these triggers are available in SQL Server 2005. DML like I said are only 3 types, Insert update and delete, but if you read books online, you will see, there are many many types for DDL Triggers. This is very good feature in SQL Server 2005. Since these triggers are not for a specific table, they cannot be placed under tables( in SSMS, object Explorer) , so I assume designer found it very handy to keep these triggers under programmability, and names these triggers as Database triggers, since these triggers are at database level but not for a specific table.
Here is a very quick example for both of these triggers,
– Create two tables
create table example1 ( eid1 int )
create table example2 ( eid2 int )
– create insert trigger on table : example1
create trigger Tr_ex1
on example1
for insert
as
begin
insert into example2 select * from inserted
end
– insert into table: example1
insert into example1 values ( 1)
– check table: example2 if this value is inserted
select * from example2
By this time you should be able to see this trigger ( Tr_ex1) under table name example1. ( in SSMS , object explorer). Right click on trigger and click modify, you will see the same SQL code as above, if you want to modify, you can modify and click execute and you are done.
– create another table to store information about, who dropped tables, when and from which database and at what time.
create table example3 ( UserName varchar (max), databasename varchar(max), Date_time datetime)
– create DDL trigger
CREATE TRIGGER Tr_ex2
ON DATABASE
FOR DROP_TABLE
AS
insert into example3 select suser_name() , db_name(), getdate()
By this time you should be able to see a database trigger, under programmability by name Tr_ex2. ( in SSMS , object explorer). Right click on trigger and click modify, you will see the same SQL code as above, if you want to modify, you can modify and click execute and you are done.
– Drop table: example2
drop table example2
– check table:example3 if it shows information about who dropped table from which database and at what time.
select * from example3
Result:
IMRAN\imran databasename 2008-08-23 23:10:09.617
Read more about DDL Triggers : http://blog.sqlauthority.com/2007/07/24/sql-server-2005-server-and-database-level-ddl-triggers-examples-and-explanation/
Hope this helps,
Imran.
Anyone else with useful articles on SQL 2005 triggers please feel free to paste it in the comments section or email me the link so I can add it on here!