-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_ex3.sql
100 lines (97 loc) · 3.23 KB
/
trigger_ex3.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
CREATE TABLE LogOperacoes (
IdLog INT IDENTITY(1,1) PRIMARY KEY,
DataHora DATETIME DEFAULT GETDATE(),
TipoOperacao CHAR(1) CHECK (TipoOperacao IN ('D', 'I', 'U')),
NomeUsuario VARCHAR(100) DEFAULT SUSER_NAME(),
DescricaoOperacao VARCHAR(200),
TabelaAfetada VARCHAR(50),
CodigoRegistro INT
);
GO
-- Trigger para a tabela Conta
CREATE TRIGGER tr_LogConta ON Conta
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @TipoOperacao CHAR(1);
SET @TipoOperacao =
CASE
WHEN EXISTS (SELECT 1 FROM inserted) AND EXISTS (SELECT 1 FROM deleted) THEN 'U'
WHEN EXISTS (SELECT 1 FROM inserted) THEN 'I'
ELSE 'D'
END;
INSERT INTO LogOperacoes (TipoOperacao, DescricaoOperacao, TabelaAfetada, CodigoRegistro)
SELECT
@TipoOperacao,
CASE
WHEN @TipoOperacao = 'U' THEN 'Atualização de conta'
WHEN @TipoOperacao = 'I' THEN 'Inserção de conta'
ELSE 'Exclusão de conta'
END,
'Conta',
CASE
WHEN @TipoOperacao IN ('U', 'D') THEN deleted.CodigoConta
ELSE inserted.CodigoConta
END
FROM inserted
FULL OUTER JOIN deleted ON inserted.CodigoConta = deleted.CodigoConta;
END;
GO
-- Trigger para tabela Lancamento
CREATE TRIGGER tr_LogLancamento ON Lancamento
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @TipoOperacao CHAR(1);
SET @TipoOperacao =
CASE
WHEN EXISTS (SELECT 1 FROM inserted) AND EXISTS (SELECT 1 FROM deleted) THEN 'U'
WHEN EXISTS (SELECT 1 FROM inserted) THEN 'I'
ELSE 'D'
END;
INSERT INTO LogOperacoes (TipoOperacao, DescricaoOperacao, TabelaAfetada, CodigoRegistro)
SELECT
@TipoOperacao,
CASE
WHEN @TipoOperacao = 'U' THEN 'Atualização de lançamento'
WHEN @TipoOperacao = 'I' THEN 'Inserção de lançamento'
ELSE 'Exclusão de lançamento'
END,
'Lancamento',
CASE
WHEN @TipoOperacao IN ('U', 'D') THEN deleted.CodigoLancamento
ELSE inserted.CodigoLancamento
END
FROM inserted
FULL OUTER JOIN deleted ON inserted.CodigoLancamento = deleted.CodigoLancamento;
END;
GO
-- Trigger for table Saldo
CREATE TRIGGER tr_LogSaldo ON Saldo
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @TipoOperacao CHAR(1);
SET @TipoOperacao =
CASE
WHEN EXISTS (SELECT 1 FROM inserted) AND EXISTS (SELECT 1 FROM deleted) THEN 'U'
WHEN EXISTS (SELECT 1 FROM inserted) THEN 'I'
ELSE 'D'
END;
INSERT INTO LogOperacoes (TipoOperacao, DescricaoOperacao, TabelaAfetada, CodigoRegistro)
SELECT
@TipoOperacao,
CASE
WHEN @TipoOperacao = 'U' THEN 'Atualização de saldo'
WHEN @TipoOperacao = 'I' THEN 'Inserção de saldo'
ELSE 'Exclusão de saldo'
END,
'Saldo',
CASE
WHEN @TipoOperacao IN ('U', 'D') THEN deleted.CodigoConta
ELSE inserted.CodigoConta
END
FROM inserted
FULL OUTER JOIN deleted ON inserted.CodigoConta = deleted.CodigoConta;
END;
GO