-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathADMIN - SQL Server Service Restart Send Email.sql
More file actions
102 lines (90 loc) · 3.8 KB
/
ADMIN - SQL Server Service Restart Send Email.sql
File metadata and controls
102 lines (90 loc) · 3.8 KB
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
USE [msdb]
GO
/****** Object: Job [ADMIN - SQL Server Service Restart Send Email] Script Date: 6/19/2018 9:15:22 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [Database Maintenance] Script Date: 6/19/2018 9:15:22 PM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'ADMIN - SQL Server Service Restart Send Email',
@enabled=1,
@notify_level_eventlog=2,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'This job starts when SQL Server Agent Service start to notify for possible server reboot, service restart which migh need to be investigated if it''s unexpected.',
@category_name=N'Database Maintenance',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [DBA Send Email For SQL Services Restart] Script Date: 6/19/2018 9:15:23 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'DBA Send Email For SQL Services Restart',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'USE msdb
GO
-- Declare variables for necessary email content
DECLARE @ServerName VARCHAR(128),
@ComputerNamePhysicalNetBIOS VARCHAR(128),
@Datetime DATETIME,
@EmailRecipients VARCHAR(256),
@EmailSubject VARCHAR(128),
@MessageBody VARCHAR(512);
SELECT @EmailRecipients = email_address + '';bgucuk@ssbinfo.com'' -- if more than one email address use ; between email addresses
FROM dbo.sysoperators
WHERE name = ''DBA'';
--SELECT @EmailRecipients
-- Set variables to proper values
SELECT @EmailSubject = ''SQL Server Services Have Been Started!!!'',
@ComputerNamePhysicalNetBIOS = CAST(SERVERPROPERTY(''ComputerNamePhysicalNetBIOS'') AS VARCHAR(128)),
@ServerName = CAST(SERVERPROPERTY(''ServerName'') AS VARCHAR(128)),
@Datetime = sqlserver_start_time
FROM sys.dm_os_sys_info;
SELECT @MessageBody = ''SQL Server service has been started on a SQL Server Instance named '' + @ServerName + CHAR(13) +
''running on windows server '' + @ComputerNamePhysicalNetBIOS + '' on '' + CAST(@Datetime AS VARCHAR(20)) + ''.'' + CHAR(13) + CHAR(13) +
''Investigate the service restart if it has not been communicated.'';
--SELECT @MessageBody
EXEC sp_send_dbmail
@recipients = @EmailRecipients,
@subject = @EmailSubject,
@body = @MessageBody,
@body_format = ''TEXT'';
',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'ADMIN - SQL Server Service Restart Send Email',
@enabled=1,
@freq_type=64,
@freq_interval=0,
@freq_subday_type=0,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20180619,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO