Eae Pessoal,
Estou me preparando para fazer a prova 70-431 para me tornar MCTS em Sql Server. Lendo o livro MCTS 70-431 Self-Paced Training Kit eu encontrei um excelente exemplo de como criar uma trigger (gatilho) quando uma determinada tabela for alterada:
CREATE TRIGGER tu_employeepayaudit
ON dbo.Employee
FOR UPDATE
AS
DECLARE @now DATETIME
SET @now = getdate()
BEGIN TRY
INSERT INTO dbo.EmployeeAudit
(RowImage, PayRate, ChangeDate, ChangeUser)
SELECT ‘BEFORE’, INSERTED.PayRate, @now, suser_sname()
FROM DELETED
INSERT INTO dbo.EmployeeAudit
(RowImage, PayRate, ChangeDate, ChangeUser)
SELECT ‘AFTER’, INSERTED.PayRate, @now, suser_sname()
FROM INSERTED
END TRY
BEGIN CATCH
–Some error handling code
ROLLBACK TRANSACTION
END CATCH
att:
