forked from way0utwest/usingcitopreventdbproblems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
62_isolation_setup.sql
92 lines (77 loc) · 2.23 KB
/
62_isolation_setup.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
/*
Testing T-SQL Made Easy - Isolation
Copyright 2015, Sebastian Meine and Steve Jones
This code is provided as is for demonstration purposes. It may not be suitable for
your environment. Please test this on your own systems. This code may not be republished
or redistributed by anyone without permission.
You are free to use this code inside of your own organization.
*/
USE TestingTSQL
GO
-- clear old objects
IF OBJECT_ID('dbo.MonthlyNotificationRecipients') IS NOT NULL DROP TABLE dbo.MonthlyNotificationRecipients;
IF OBJECT_ID('dbo.SendMonthlyNotifications') IS NOT NULL DROP PROCEDURE dbo.SendMonthlyNotifications;
IF OBJECT_ID('dbo.SendMonthlyNotificationEmail') IS NOT NULL DROP PROCEDURE dbo.SendMonthlyNotificationEmail;
IF OBJECT_ID('dbo.MonthlyNotificationRecipients') IS NOT NULL DROP TABLE dbo.MonthlyNotificationRecipients;
GO
-- create new objects
CREATE TABLE dbo.MonthlyNotificationRecipients(
name NVARCHAR(200) PRIMARY KEY CLUSTERED,
email NVARCHAR(200)
);
GO
INSERT dbo.MonthlyNotificationRecipients
VALUES ('Steve', '[email protected]')
, ('Uma', '[email protected]')
, ('Deuce', '[email protected]')
go
-- procedure to send emails
CREATE PROCEDURE dbo.SendMonthlyNotificationEmail
@recipient_name NVARCHAR(200),
@recipient_email NVARCHAR(200)
AS
BEGIN
RAISERROR('TODO: Implement email functionality',16,10);
END;
GO
-- Procedure to load emails and send
CREATE PROCEDURE dbo.SendMonthlyNotifications
AS
BEGIN
DECLARE cur CURSOR LOCAL FAST_FORWARD FOR
SELECT MNR.email, MNR.name
FROM dbo.MonthlyNotificationRecipients AS MNR
ORDER BY MNR.name;
DECLARE @name NVARCHAR(200);
DECLARE @email NVARCHAR(200);
OPEN cur;
WHILE(1=1)
BEGIN
FETCH NEXT FROM cur INTO @email, @name;
IF(@@FETCH_STATUS <> 0)
BREAK;
--
-- Email has the following format:
--
--
-- TO: {email}
-- SUBJECT: your monthly report
-- BODY:
--
-- Dear {name}
--
-- [include the report here]
--
-- please let us know immediately,
-- if there are any concerns with the data.
--
-- Sincerely
--
-- Dr. Boo
--
EXEC dbo.SendMonthlyNotificationEmail @recipient_name = @name, @recipient_email = @email;
--Get next record
-- FETCH NEXT FROM cur INTO @email, @name;
END
END;
GO