Sqlserver Class22 (TSQL 5)
Sqlserver Class22 (TSQL 5)
special kind of SP, which is executed automatically when some event occurs in
the database.
DML Triggers
------------
// Trigger code
END
Note : you can have multiple AFTER DML triggers for the same event and on the same
object
Magic Tables
INSERTED and DELETED
these are accessed only with in the scope of triggers.
whenever we do Inserting the data , INSERETED will contain the new data, DELETED
will not contain anything
whenever we do delete , INSERETED will not contain the any data, DELETED table will
contain the old data
whenever we do update, INSERETED will contain the new data, DELETED table will
contain the old data
GO
GO
--
sp_helptrigger 'Employee'
GO
6302069792
Qb27m1pIP
PNR- 4146807549
Return -- 4346690727
CREATE TRIGGER Trg_Employee_I_U_D
ON Employee
AFTER INSERT,UPDATE,DELETE
AS
BEGIN
END
CREATE TRIGGER Trg_Employee_I
ON Employee
AFTER INSERT
AS
BEGIN
INSERT INTO
Employee_Audit(EmpId,New_EmpName,New_EmpDesignation,Username,insertedDat
e)
SELECT EmpId,EmpName,EmpDesignation,ORIGINAL_LOGIN(),GETDATE()
FROM INSERTED
END
IF (UPDATE(EmpId))
Begin
Rollback
RAISError('cannot update EmployeeId',16,1)
Return
End
-- INSERT INTO
Employee_Audit(EmpId,Old_EmpName,New_EmpName,Old_EmpDesignation,New_E
mpDesignation,Username,insertedDate)
--SELECT EmpId,EmpName,EmpDesignation,ORIGINAL_LOGIN(),GETDATE()
FROM INSERTED
END
IF (UPDATE(EmpId))
Begin
Rollback
RAISError('cannot update EmployeeId',16,1)
Return
End
END
IF (UPDATE(EmpId))
Begin
Rollback
RAISError('cannot update EmployeeId',16,1)
Return
End
END
sp_helptrigger 'Employee'