2. Microsoft SQL Server provides two primary
mechanisms for enforcing business rules and
data integrity: constraints and triggers. A
trigger is a special type of stored procedure
that automatically takes effect when a
language event executes. SQL Server
includes three general types of triggers: DML
triggers, DDL triggers, and logon triggers.
3. DDL triggers are invoked when a data definition language
(DDL) event takes place in the server or database.
DML triggers are invoked when a data manipulation
language (DML) event takes place in the database. DML
events include INSERT, UPDATE, or DELETE statements
that modify data in a specified table or view. A DML
trigger can query other tables and can include complex
Transact-SQL statements.The trigger and the statement
that fires it are treated as a single transaction, which can
be rolled back from within the trigger. If a severe error is
detected (for example, insufficient disk space), the entire
transaction automatically rolls back.
4. They can cascade changes through related tables in the database;
however, these changes can be executed more efficiently using
cascading referential integrity constraints.
They can guard against malicious or incorrect INSERT, UPDATE,
and DELETE operations and enforce other restrictions that are
more complex than those defined with CHECK constraints.
Unlike CHECK constraints, DML triggers can reference columns in
other tables. For example, a trigger can use a SELECT from
another table to compare to the inserted or updated data and to
perform additional actions, such as modify the data or display a
user-defined error message.
They can evaluate the state of a table before and after a data
modification and take actions based on that difference.
Multiple DML triggers of the same type (INSERT, UPDATE, or
DELETE) on a table allow multiple, different actions to take place
in response to the same modification statement.
5. AFTERTriggers
AFTER triggers are executed after the action of the
INSERT, UPDATE, or DELETE statement is performed.
Specifying AFTER is the same as specifying FOR,
which is the only option available in earlier versions of
Microsoft SQL Server.AFTER triggers can be specified
only on tables.
INSTEAD OFTriggers
INSTEAD OF triggers are executed in place of the
usual triggering action. INSTEAD OF triggers can also
be defined on views with one or more base tables,
where they can extend the types of updates a view
can support.
6. TRIGGERS
1. when you create a trigger
you have to identify event
and action of your trigger.
2. trigger is run automatically
if the event is occurred.
3. within a trigger you can call
specific s.p.
STORED PROCEDURES
1. when you create s.p you
don't identify event and
action.
2. s.p don't run automatically
but you have to run it
manually.
3. within a s.p you cannot call
a trigger.
7. CREATETABLE Employee_Test
(
Emp_ID INT Identity,
Emp_nameVarchar(100),
Emp_Sal Decimal (10,2)
)
INSERT INTO Employee_TestVALUES ('Anees',1000);
INSERT INTO Employee_TestVALUES ('Rick',1200);
INSERT INTO Employee_TestVALUES ('John',1100);
INSERT INTO Employee_TestVALUES
('Stephen',1300);
INSERT INTO Employee_TestVALUES ('Maria',1400);
9. CREATETRIGGER trgAfterInsert ON Employee_Test FOR INSERT
AS declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);
select @empid=i.Emp_ID from inserted i;
select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;
set @audit_action='Inserted Record -- After InsertTrigger.';
insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER INSERT trigger fired.‘
10. CREATETRIGGER trgAfterUpdate ON Employee_Test
FOR UPDATE AS declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);
select @empid=i.Emp_ID from inserted i;
if update(Emp_Name)
select @empname=i.Emp_Name from inserted i;
set @audit_action='Updated Record -- After UpdateTrigger.';
if update(Emp_Sal)
select @empsal=i.Emp_Sal from inserted i;
set @audit_action='Updated Record -- After UpdateTrigger.';
insert into Employee_Test_Audit
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER UPDATETrigger fired.'
11. CREATETRIGGER trgAfterDelete ON Employee_Test AFTER
DELETE AS declare
@empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);
select @empid=d.Emp_ID from deleted d;
select @empname=d.Emp_Name from deleted d;
select @empsal=d.Emp_Sal from deleted d;
set @audit_action='Deleted -- After DeleteTrigger.';
insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER DELETETRIGGER fired.'