Articles → SQL SERVER → Triggers In SQL Server

Triggers In SQL Server






Purpose







Syntax


Create trigger < trigger_Name > on {table | View} {for, 
after 
  , 
  instead of} {[insert], 
  [update], 
  [delete]} As < Sql_Statements >



Example




PK_IDsNamesAddress
1Name1Address1
2Name2Address2



Insert Trigger




Create trigger trg_Insert_tbl_Test on tbl_Test For insert as Begin 
select 
  * 
from 
  inserted End




Insert into tbl_Test 
values 
  ('name3', 'address3')






PK_IDsNamesAddress
3Name3Address3



Update Trigger




Create trigger trg_Update_tbl_Test on tbl_Test For 
Update 
  as Begin 
select 
  * 
from 
  deleted 
select 
  * 
from 
  inserted End




Update 
  tbl_Test 
Set 
  sAddress = 'TestAddress' 
where 
  PK_ID = 3






PK_IDsNamesAddress
3Name3Address3




PK_IDsNamesAddress
3Name3TestAddress



Delete Trigger





Posted By  -  Karan Gupta
 
Posted On  -  Monday, November 1, 2010

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250