forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.usp_SQL_Server_System_Report.sql
3135 lines (2529 loc) · 80.8 KB
/
dbo.usp_SQL_Server_System_Report.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON
-----------------------------------------------------------------------------------------------------------------------------
-- Error Trapping: Check If Procedure Already Exists And Create Shell If Applicable
-----------------------------------------------------------------------------------------------------------------------------
IF OBJECT_ID (N'dbo.usp_SQL_Server_System_Report', N'P') IS NULL
BEGIN
EXECUTE ('CREATE PROCEDURE dbo.usp_SQL_Server_System_Report AS SELECT 1 AS shell')
END
GO
-----------------------------------------------------------------------------------------------------------------------------
-- Stored Procedure Details: Listing Of Standard Details Related To The Stored Procedure
-----------------------------------------------------------------------------------------------------------------------------
-- Purpose: SQL Server System Report
-- Create Date (MM/DD/YYYY): 05/12/2010
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-----------------------------------------------------------------------------------------------------------------------------
-- Modification History: Listing Of All Modifications Since Original Implementation
-----------------------------------------------------------------------------------------------------------------------------
-- Description: Added "@Output_Mode" And "@Unused_Index_Days" Input Parameters
-- : Code Restructuring For Better Query Performance
-- : Minor Changes To Code Style
-- : Added "connections""first_day_of_week", "is_clustered", And "windows_version" To "Instance Information" Section
-- Date (MM/DD/YYYY): 12/22/2011
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-- Description: Added "create_date" And "file_name" To "Database Details" Section
-- : Added "backup_finish_date", "database_version", "machine_name", And "server_name" To "Last Backup Set Details" Section
-- : "Last Backup Set Details" Section Now Only Shows Information For Databases Currently On The Instance
-- : Added "schema_name" To "Unused Indexes" Section
-- : Extended "CONVERT" Character Length To Deal With "Msg 234" Error
-- : Rewrote Time Calculation Logic
-- Date (MM/DD/YYYY): 01/18/2012
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-- Description: Added Additional Input Parameters
-- : Reformatted Code
-- : Bug Fixes
-- : Added "Instance Settings", "Index Fragmentation", "Missing Indexes" Sections
-- Date (MM/DD/YYYY): 09/20/2013
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-- Description: Renamed Some Headings / Variables / Temp Tables
-- : Bug Fixes
-- : Enhanced HTML Output
-- : "Index" Ouputs Now Include Indexed Views
-- : Added "instance_name", "product_name", And "server_memory_mb" To "Instance Information" Section
-- : Added "authentication", "date_format", "fill_factor_percentage", "is_compressed_backups_enabled", "is_optimize_for_ad_hoc_workloads_enabled""is_scan_for_startup_procs_enabled", "is_sql_mail_enabled", "language", And "locale_id" To "Instance Settings" Section
-- : Added "cached_mb" To "Database Summary" Section
-- : Added "database_owner", "drive_letter", "file_name", "file_path", "file_read_mb", "file_reads", "file_writes", "file_written_mb", "io_wait_time_reads", And "io_wait_time_writes" To "Database Details" Section
-- : Added "physical_device_name" To "Last Backup Set Details" Section
-- : Added "category", "class", "date_created", "date_modified", "job_owner", "type", And "version" To "SQL Server Agent Jobs (Last 24 Hours)" Section
-- : Added "filter_definition", "include_key", "object_type", And "row_count" To "Index Fragmentation" Section
-- : Added "object_type" And "row_count" To "Missing Indexes" Section
-- : Added "filter_definition", "include_key", "object_type", And "row_count" To "Unused Indexes" Section
-- Date (MM/DD/YYYY): 12/11/2013
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-- Description: Major Revamp Of "Database Summary" And "Database Details" Sections
-- Date (MM/DD/YYYY): 07/11/2014
-- Developer: Sean Smith (s.smith.sql AT gmail DOT com)
-- Additional Notes: N/A
-----------------------------------------------------------------------------------------------------------------------------
-- Main Query: Create Procedure
-----------------------------------------------------------------------------------------------------------------------------
ALTER PROCEDURE dbo.usp_SQL_Server_System_Report
@Output_Mode AS CHAR (1) = NULL
,@Unused_Index_Days AS INT = 7
,@Recipients AS VARCHAR (MAX) = NULL
,@Copy_Recipients AS VARCHAR (MAX) = NULL
,@Instance_Information AS BIT = 1
,@Instance_Settings AS BIT = 1
,@Drives_Space AS BIT = 1
,@Database_Summary AS BIT = 1
,@Database_Details AS BIT = 1
,@Last_Backup AS BIT = 1
,@Agent_Jobs AS BIT = 1
,@Fragmentation AS BIT = 1
,@Missing_Indexes AS BIT = 1
,@Unused_Indexes AS BIT = 1
AS
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
SET ARITHIGNORE ON
SET TEXTSIZE 2147483647
DECLARE
@Avoid_Truncation AS NVARCHAR (MAX)
,@Body AS NVARCHAR (MAX)
,@Database_ID AS INT
,@Database_Name_Loop AS NVARCHAR (500)
,@Date_24_Hours_Ago AS DATETIME
,@Date_Now AS DATETIME
,@Filter_Definition_String AS NVARCHAR (50)
,@Server_Memory_MB AS BIGINT
,@SQL_Server_Start_Time AS DATETIME
,@SQL_String AS NVARCHAR (MAX)
,@Subject AS NVARCHAR (255)
,@XML_String AS NVARCHAR (MAX)
DECLARE @Fixed_Drives_Free_Space AS TABLE
(
drive_letter VARCHAR (5) PRIMARY KEY CLUSTERED
,free_space_mb BIGINT
)
SET @Avoid_Truncation = N''
SET @Body = N''
SET @Date_24_Hours_Ago = GETDATE () - 1
SET @Date_Now = @Date_24_Hours_Ago + 1
SET @Subject = N'SQL Server System Report: ' + @@SERVERNAME
SET @XML_String = N''
IF EXISTS (SELECT * FROM master.sys.all_columns AC WHERE AC.[object_id] = OBJECT_ID (N'master.sys.dm_os_sys_info', N'V') AND AC.name = N'sqlserver_start_time')
BEGIN
SET @SQL_String =
N'
SELECT
@SQL_Server_Start_Time = DOSI.sqlserver_start_time
FROM
master.sys.dm_os_sys_info DOSI
'
EXECUTE sys.sp_executesql
@SQL_String
,N'
@SQL_Server_Start_Time AS DATETIME OUTPUT
'
,@SQL_Server_Start_Time OUTPUT
END
ELSE BEGIN
SET @SQL_Server_Start_Time =
(
SELECT
DB.create_date
FROM
master.sys.databases DB
WHERE
DB.name = N'tempdb'
)
END
IF DATEDIFF (SECOND, @SQL_Server_Start_Time, GETDATE ()) < (@Unused_Index_Days * 86400)
BEGIN
SET @Unused_Indexes = 0
END
IF EXISTS (SELECT * FROM master.sys.all_columns AC WHERE AC.[object_id] = OBJECT_ID (N'master.sys.dm_os_sys_info', N'V') AND AC.name = N'physical_memory_kb')
BEGIN
SET @SQL_String =
N'
SELECT
@Server_Memory_MB = DOSI.physical_memory_kb / 1024
FROM
master.sys.dm_os_sys_info DOSI
'
END
ELSE BEGIN
SET @SQL_String =
N'
SELECT
@Server_Memory_MB = DOSI.physical_memory_in_bytes / 1048576
FROM
master.sys.dm_os_sys_info DOSI
'
END
EXECUTE sys.sp_executesql
@SQL_String
,N'
@Server_Memory_MB AS BIGINT OUTPUT
'
,@Server_Memory_MB OUTPUT
IF EXISTS (SELECT * FROM master.sys.all_columns AC WHERE AC.[object_id] = OBJECT_ID (N'master.sys.indexes', N'V') AND AC.name = N'filter_definition')
BEGIN
SET @Filter_Definition_String = N'ISNULL (I.filter_definition, N'''')'
END
ELSE BEGIN
SET @Filter_Definition_String = N''''''
END
-----------------------------------------------------------------------------------------------------------------------------
-- Error Trapping: Check If Temp Table(s) Already Exist(s) And Drop If Applicable
-----------------------------------------------------------------------------------------------------------------------------
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_databases', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_databases
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_row_counts', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_row_counts
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_instance_information', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_instance_information
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_instance_settings', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_instance_settings
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_database_summary', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_database_summary
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_database_details', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_database_details
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_last_backup_set', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_last_backup_set
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_agent_jobs', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_agent_jobs
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_index_fragmentation', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_index_fragmentation
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_missing_indexes', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_missing_indexes
END
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_unused_indexes', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_unused_indexes
END
-----------------------------------------------------------------------------------------------------------------------------
-- Temp Table: Database List
-----------------------------------------------------------------------------------------------------------------------------
CREATE TABLE dbo.#temp_sssr_databases
(
database_name NVARCHAR (128) PRIMARY KEY CLUSTERED
,database_owner NVARCHAR (128)
,recovery_model NVARCHAR (60)
,[compatibility_level] VARCHAR (3)
,create_date DATETIME
,collation_name NVARCHAR (128)
)
INSERT INTO dbo.#temp_sssr_databases
(
database_name
,database_owner
,recovery_model
,[compatibility_level]
,create_date
,collation_name
)
SELECT
DB.name AS database_name
,SUSER_SNAME (DB.owner_sid) AS database_owner
,DB.recovery_model_desc AS recovery_model
,DB.[compatibility_level]
,DB.create_date
,DB.collation_name
FROM
master.sys.databases DB
WHERE
DB.[state] = 0
AND DB.is_read_only = 0
AND DB.is_in_standby = 0
AND DB.source_database_id IS NULL
-----------------------------------------------------------------------------------------------------------------------------
-- Temp Table: Row Counts Per Table Per Database
-----------------------------------------------------------------------------------------------------------------------------
IF (@Fragmentation = 1 OR @Missing_Indexes = 1 OR @Unused_Indexes = 1)
BEGIN
CREATE TABLE dbo.#temp_sssr_row_counts
(
database_id INT NOT NULL
,[object_id] INT NOT NULL
,[rows] BIGINT
)
SET @Database_Name_Loop =
(
SELECT TOP (1)
X.database_name
FROM
dbo.#temp_sssr_databases X
ORDER BY
X.database_name
)
WHILE @Database_Name_Loop IS NOT NULL
BEGIN
SET @SQL_String = @Avoid_Truncation +
N'
USE [' + @Database_Name_Loop + N']
INSERT INTO dbo.#temp_sssr_row_counts
(
database_id
,[object_id]
,[rows]
)
SELECT
DB_ID () AS database_id
,P.[object_id]
,SUM (P.[rows]) AS total_rows
FROM
sys.partitions P
WHERE
P.index_id IN (0, 1)
GROUP BY
P.[object_id]
'
EXECUTE (@SQL_String)
SET @Database_Name_Loop =
(
SELECT TOP (1)
X.database_name
FROM
dbo.#temp_sssr_databases X
WHERE
X.database_name > @Database_Name_Loop
ORDER BY
X.database_name
)
END
ALTER TABLE dbo.#temp_sssr_row_counts ADD PRIMARY KEY CLUSTERED (database_id, [object_id])
END
-----------------------------------------------------------------------------------------------------------------------------
-- Main Query I: Instance Information
-----------------------------------------------------------------------------------------------------------------------------
IF @Instance_Information = 0
BEGIN
GOTO Skip_Instance_Information
END
SELECT
SERVERPROPERTY (N'ComputerNamePhysicalNetBIOS') AS netbios_name
,@@SERVERNAME AS server_name
,ISNULL (SERVERPROPERTY (N'InstanceName'), N'Default Instance') AS instance_name
,REPLACE (REPLACE (cjPNL.product_name, N' (' + cjPNL.[level] + N')', N''), N'Microsoft ', N'') AS product_name
,REPLACE (CONVERT (NVARCHAR (128), SERVERPROPERTY (N'Edition')), N' Edition', N'') AS edition
,SERVERPROPERTY (N'ProductVersion') AS [version]
,cjPNL.[level]
,(CASE SERVERPROPERTY (N'IsClustered')
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_clustered
,CONVERT (NVARCHAR (19), @SQL_Server_Start_Time, 120) AS online_since
,(CASE
WHEN oaDSR.total_days = '0' THEN '_'
ELSE oaDSR.total_days
END) + ' Day(s) ' + (CASE
WHEN oaDSR.seconds_remaining = 0 THEN '__:__:__'
WHEN oaDSR.seconds_remaining < 60 THEN '__:__:' + RIGHT (oaDSR.total_seconds, 2)
WHEN oaDSR.seconds_remaining < 3600 THEN '__:' + RIGHT (oaDSR.total_seconds, 5)
ELSE oaDSR.total_seconds
END) AS uptime
,SERVERPROPERTY (N'ProcessID') AS process_id
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, @@CONNECTIONS), 1)), 4, 23)) AS connections
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, @@TOTAL_READ), 1)), 4, 23)) AS reads
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, @@TOTAL_WRITE), 1)), 4, 23)) AS writes
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, DOSI.cpu_count), 1)), 4, 23)) AS logical_cpu_count
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, DOSI.cpu_count / DOSI.hyperthread_ratio), 1)), 4, 23)) AS physical_cpu_count
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, @Server_Memory_MB), 1)), 4, 23)) AS server_memory_mb
,REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (RIGHT (@@VERSION, CHARINDEX (REVERSE (N' on Windows '), REVERSE (@@VERSION)) - 1), N'Service Pack ', N'SP'), N'(', N''), N')', N''), N'<', N'('), N'>', N')') AS windows_version
INTO
dbo.#temp_sssr_instance_information
FROM
master.sys.dm_os_sys_info DOSI
CROSS JOIN
(
SELECT
LEFT (@@VERSION, CHARINDEX (N' - ', @@VERSION) - 1) AS product_name
,CONVERT (NVARCHAR (128), SERVERPROPERTY (N'ProductLevel')) AS [level]
) cjPNL
CROSS JOIN
(
SELECT
DATEDIFF (SECOND, @SQL_Server_Start_Time, GETDATE ()) AS uptime_seconds
) sqUTS
OUTER APPLY
(
SELECT
CONVERT (VARCHAR (5), sqUTS.uptime_seconds / 86400) AS total_days
,CONVERT (CHAR (8), DATEADD (SECOND, sqUTS.uptime_seconds % 86400, 0), 108) AS total_seconds
,sqUTS.uptime_seconds % 86400 AS seconds_remaining
) oaDSR
IF @@ROWCOUNT = 0
BEGIN
GOTO Skip_Instance_Information
END
IF @Output_Mode = 'E'
BEGIN
SET @XML_String =
CONVERT (NVARCHAR (MAX),
(
SELECT
'',X.netbios_name AS 'td'
,'',X.server_name AS 'td'
,'',X.instance_name AS 'td'
,'',X.product_name AS 'td'
,'',X.edition AS 'td'
,'',X.[version] AS 'td'
,'',X.[level] AS 'td'
,'',X.is_clustered AS 'td'
,'',X.online_since AS 'td'
,'',X.uptime AS 'td'
,'',X.process_id AS 'td'
,'','right_align' + X.connections AS 'td'
,'','right_align' + X.reads AS 'td'
,'','right_align' + X.writes AS 'td'
,'','right_align' + X.logical_cpu_count AS 'td'
,'','right_align' + X.physical_cpu_count AS 'td'
,'','right_align' + X.server_memory_mb AS 'td'
,'',X.windows_version AS 'td'
FROM
dbo.#temp_sssr_instance_information X
FOR
XML PATH ('tr')
)
)
SET @Body = @Body +
N'
<br>
<h3><center>Instance Information</center></h3>
<center>
<table frame=box border=1 cellpadding=2>
<tr style="color: white; background-color: black">
<th nowrap>NetBIOS Name</th>
<th nowrap>Server Name</th>
<th nowrap>Instance Name</th>
<th nowrap>Product Name</th>
<th nowrap>Edition</th>
<th nowrap>Version</th>
<th nowrap>Level</th>
<th nowrap>Clustered</th>
<th nowrap>Online Since</th>
<th nowrap>Uptime</th>
<th nowrap>Process ID</th>
<th nowrap>Connections</th>
<th nowrap>Reads</th>
<th nowrap>Writes</th>
<th nowrap>Logical CPU Count</th>
<th nowrap>Physical CPU Count</th>
<th nowrap>Server Memory (MB)</th>
<th nowrap>Windows Version</th>
</tr>
'
SET @Body = @Body + @XML_String +
N'
</table>
</center>
'
END
ELSE BEGIN
SELECT
X.netbios_name
,X.server_name
,X.instance_name
,X.product_name
,X.edition
,X.[version]
,X.[level]
,X.is_clustered AS [clustered]
,X.online_since
,X.uptime
,X.process_id
,X.connections
,X.reads
,X.writes
,X.logical_cpu_count
,X.physical_cpu_count
,X.server_memory_mb
,X.windows_version
FROM
dbo.#temp_sssr_instance_information X
END
Skip_Instance_Information:
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_instance_information', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_instance_information
END
-----------------------------------------------------------------------------------------------------------------------------
-- Main Query II: Instance Settings
-----------------------------------------------------------------------------------------------------------------------------
IF @Instance_Settings = 0
BEGIN
GOTO Skip_Instance_Settings
END
SELECT
(CASE SERVERPROPERTY (N'IsIntegratedSecurityOnly')
WHEN 0 THEN 'Mixed Mode'
WHEN 1 THEN 'Windows'
ELSE 'N/A'
END) AS [authentication]
,SL.alias + N': ' + SL.name AS [language]
,SL.lcid AS locale_id
,SERVERPROPERTY (N'Collation') AS collation
,SL.[dateformat] AS date_format
,(CASE SL.[datefirst]
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'N/A'
END) AS first_day_of_week
,(CASE SERVERPROPERTY (N'IsFullTextInstalled')
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_full_text_installed
,(CASE cjCFG.is_show_advanced_options_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_show_advanced_options_enabled
,(CASE cjCFG.is_clr_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_clr_enabled
,(CASE cjCFG.is_compressed_backups_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_compressed_backups_enabled
,(CASE cjCFG.is_scan_for_startup_procs_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_scan_for_startup_procs_enabled
,(CASE cjCFG.is_optimize_for_ad_hoc_workloads_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_optimize_for_ad_hoc_workloads_enabled
,(CASE cjCFG.is_xp_cmdshell_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_xp_cmdshell_enabled
,(CASE cjCFG.is_sql_mail_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_sql_mail_enabled
,(CASE cjCFG.is_database_mail_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_database_mail_enabled
,(CASE cjCFG.is_default_trace_enabled
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'N/A'
END) AS is_default_trace_enabled
,CONVERT (VARCHAR (3), (CASE
WHEN cjCFG.fill_factor_percentage = 0 THEN 100
ELSE cjCFG.fill_factor_percentage
END)) AS fill_factor_percentage
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, cjCFG.min_server_memory_mb), 1)), 4, 23)) AS min_server_memory_mb
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, cjCFG.max_server_memory_mb), 1)), 4, 23)) AS max_server_memory_mb
INTO
#temp_sssr_instance_settings
FROM
master.sys.syslanguages SL
CROSS JOIN
(
SELECT
MAX (CASE
WHEN CFG.configuration_id = 518 THEN CFG.value
END) AS is_show_advanced_options_enabled
,MAX (CASE
WHEN CFG.configuration_id = 1562 THEN CFG.value
END) AS is_clr_enabled
,MAX (CASE
WHEN CFG.configuration_id = 1579 THEN CFG.value
END) AS is_compressed_backups_enabled
,MAX (CASE
WHEN CFG.configuration_id = 1547 THEN CFG.value
END) AS is_scan_for_startup_procs_enabled
,MAX (CASE
WHEN CFG.configuration_id = 1581 THEN CFG.value
END) AS is_optimize_for_ad_hoc_workloads_enabled
,MAX (CASE
WHEN CFG.configuration_id = 16390 THEN CFG.value
END) AS is_xp_cmdshell_enabled
,MAX (CASE
WHEN CFG.configuration_id = 16385 THEN CFG.value
END) AS is_sql_mail_enabled
,MAX (CASE
WHEN CFG.configuration_id = 16386 THEN CFG.value
END) AS is_database_mail_enabled
,MAX (CASE
WHEN CFG.configuration_id = 1568 THEN CFG.value
END) AS is_default_trace_enabled
,MAX (CASE
WHEN CFG.configuration_id = 109 THEN CFG.value
END) AS fill_factor_percentage
,MAX (CASE
WHEN CFG.configuration_id = 1543 THEN CFG.value
END) AS min_server_memory_mb
,MAX (CASE
WHEN CFG.configuration_id = 1544 THEN CFG.value
END) AS max_server_memory_mb
FROM
master.sys.configurations CFG
WHERE
CFG.configuration_id IN (109, 518, 1543, 1544, 1547, 1562, 1568, 1579, 1581, 16385, 16386, 16390)
) cjCFG
WHERE
SL.lcid = SERVERPROPERTY (N'LCID')
IF @@ROWCOUNT = 0
BEGIN
GOTO Skip_Instance_Settings
END
IF @Output_Mode = 'E'
BEGIN
SET @XML_String =
CONVERT (NVARCHAR (MAX),
(
SELECT
'',X.[authentication] AS 'td'
,'',X.[language] AS 'td'
,'',X.locale_id AS 'td'
,'',X.collation AS 'td'
,'',X.date_format AS 'td'
,'',X.first_day_of_week AS 'td'
,'',X.is_full_text_installed AS 'td'
,'',X.is_show_advanced_options_enabled AS 'td'
,'',X.is_clr_enabled AS 'td'
,'',X.is_compressed_backups_enabled AS 'td'
,'',X.is_scan_for_startup_procs_enabled AS 'td'
,'',X.is_optimize_for_ad_hoc_workloads_enabled AS 'td'
,'',X.is_xp_cmdshell_enabled AS 'td'
,'',X.is_sql_mail_enabled AS 'td'
,'',X.is_database_mail_enabled AS 'td'
,'',X.is_default_trace_enabled AS 'td'
,'','right_align' + X.fill_factor_percentage AS 'td'
,'','right_align' + X.min_server_memory_mb AS 'td'
,'','right_align' + X.max_server_memory_mb AS 'td'
FROM
dbo.#temp_sssr_instance_settings X
FOR
XML PATH ('tr')
)
)
SET @Body = @Body +
N'
<br>
<h3><center>Instance Settings</center></h3>
<center>
<table frame=box border=1 cellpadding=2>
<tr style="color: white; background-color: black">
<th nowrap>Authentication</th>
<th nowrap>Language</th>
<th nowrap>Locale ID</th>
<th nowrap>Collation</th>
<th nowrap>Date Format</th>
<th nowrap>First Day Of Week</th>
<th nowrap>Full-Text Installed</th>
<th nowrap>Advanced Options Enabled</th>
<th nowrap>CLR Enabled</th>
<th nowrap>Compress Backups</th>
<th nowrap>Scan For Startup Procs</th>
<th nowrap>Optimize For Ad Hocs</th>
<th nowrap>Command Shell Enabled</th>
<th nowrap>SQL Mail Enabled</th>
<th nowrap>Database Mail Enabled</th>
<th nowrap>Default Trace Enabled</th>
<th nowrap>Fill Factor %</th>
<th nowrap>Minimum Memory (MB)</th>
<th nowrap>Maximum Memory (MB)</th>
</tr>
'
SET @Body = @Body + @XML_String +
N'
</table>
</center>
'
END
ELSE BEGIN
SELECT
X.[authentication]
,X.[language]
,X.locale_id
,X.collation
,X.date_format
,X.first_day_of_week
,X.is_full_text_installed AS full_text_installed
,X.is_show_advanced_options_enabled AS advanced_options_enabled
,X.is_clr_enabled AS clr_enabled
,X.is_compressed_backups_enabled AS compress_backups
,X.is_scan_for_startup_procs_enabled AS scan_for_startup_procs
,X.is_optimize_for_ad_hoc_workloads_enabled AS optimize_for_ad_hocs
,X.is_xp_cmdshell_enabled AS command_shell_enabled
,X.is_sql_mail_enabled AS sql_mail_enabled
,X.is_database_mail_enabled AS database_mail_enabled
,X.is_default_trace_enabled AS default_trace_enabled
,X.fill_factor_percentage AS fill_factor_pct
,X.min_server_memory_mb AS minimum_memory_mb
,X.max_server_memory_mb AS maximum_memory_mb
FROM
dbo.#temp_sssr_instance_settings X
END
Skip_Instance_Settings:
IF OBJECT_ID (N'tempdb.dbo.#temp_sssr_instance_settings', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#temp_sssr_instance_settings
END
-----------------------------------------------------------------------------------------------------------------------------
-- Main Query III: Fixed Drives Free Space
-----------------------------------------------------------------------------------------------------------------------------
IF @Drives_Space = 0
BEGIN
GOTO Skip_Fixed_Drives_Free_Space
END
INSERT INTO @Fixed_Drives_Free_Space
(
drive_letter
,free_space_mb
)
EXECUTE master.dbo.xp_fixeddrives
IF @@ROWCOUNT = 0
BEGIN
GOTO Skip_Fixed_Drives_Free_Space
END
IF @Output_Mode = 'E'
BEGIN
SET @XML_String =
CONVERT (NVARCHAR (MAX),
(
SELECT
'',X.drive_letter + ':' AS 'td'
,'','right_align' + REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, X.free_space_mb), 1)), 4, 23)) AS 'td'
FROM
@Fixed_Drives_Free_Space X
ORDER BY
X.drive_letter
FOR
XML PATH ('tr')
)
)
SET @Body = @Body +
N'
<br>
<h3><center>Fixed Drives Free Space</center></h3>
<center>
<table frame=box border=1 cellpadding=2>
<tr style="color: white; background-color: black">
<th nowrap>Drive Letter</th>
<th nowrap>Free Space (MB)</th>
</tr>
'
SET @Body = @Body + @XML_String +
N'
</table>
</center>
'
END
ELSE BEGIN
SELECT
X.drive_letter + ':' AS drive_letter
,REVERSE (SUBSTRING (REVERSE (CONVERT (VARCHAR (23), CONVERT (MONEY, X.free_space_mb), 1)), 4, 23)) AS free_space_mb
FROM
@Fixed_Drives_Free_Space X
ORDER BY
X.drive_letter
END
Skip_Fixed_Drives_Free_Space:
-----------------------------------------------------------------------------------------------------------------------------