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_ID | sName | sAddress |
---|
1 | Name1 | Address1 |
2 | Name2 | Address2 |
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_ID | sName | sAddress |
---|
3 | Name3 | Address3 |
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_ID | sName | sAddress |
---|
3 | Name3 | Address3 |
PK_ID | sName | sAddress |
---|
3 | Name3 | TestAddress |
Delete Trigger