-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrigger.sql
More file actions
51 lines (38 loc) · 916 Bytes
/
Trigger.sql
File metadata and controls
51 lines (38 loc) · 916 Bytes
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
USE KP
GO
ALTER TRIGGER [triggerName] on [tableName]
AFTER UPDATE, INSERT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT * FROM INSERTED)
BEGIN
DECLARE @emailInstances TABLE
(
RowNumber int,
User nVarChar(256)
)
INSERT INTO @emailInstances
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)), A.UserName
FROM INSERTED C
LEFT JOIN
dbo.aspnet_Users A ON C.UserId = A.UserId
DECLARE @Rpt_Int int;
SELECT @Rpt_Int = MAX(RowNumber) FROM @emailInstances;
WHILE @Rpt_Int >= 1
BEGIN
DECLARE @HTML NVARCHAR(MAX);
SET @HTML =
N'<H1>' + @User + ' was inserted.</H1>'
;
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'profileName',
@recipients='somebody@email.com',
@subject = 'Something Inseted',
@body = @HTML,
@body_format = 'HTML';
SELECT @Rpt_Int = MAX(RowNumber) FROM @emailInstances WHERE RowNumber < @Rpt_Int;
END
END
END
GO