@@ -265,65 +265,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
265265 RETURN ;
266266END ;
267267
268- IF @debug = 1
269- BEGIN
270- RAISERROR (' Check if we are using system_health' , 0 , 1 ) WITH NOWAIT ;
271- END ;
272- DECLARE
273- @is_system_health bit = 0 ,
274- @is_system_health_msg nchar (1 );
275-
276- SELECT
277- @is_system_health =
278- CASE
279- WHEN @session_name LIKE N ' system%health'
280- THEN 1
281- ELSE 0
282- END ,
283- @is_system_health_msg =
284- CONVERT (nchar (1 ), @is_system_health);
285-
286- IF @debug = 1
287- AND @is_system_health = 0
288- BEGIN
289- RAISERROR (' We are not using system_health' , 0 , 1 ) WITH NOWAIT ;
290- END ;
291-
292- IF @is_system_health = 1
293- BEGIN
294- RAISERROR (' For best results, consider not using system_health as your target. Re-run with @help = 1 for guidance.' , 0 , 1 ) WITH NOWAIT ;
295- END
296-
297- /*
298- Note: I do not allow logging to a table from system_health, because the set of columns
299- and available data is too incomplete, and I don't want to juggle multiple
300- table definitions.
301-
302- Logging to a table is only allowed from a blocked_process_report Extended Event,
303- but it can either be ring buffer or file target. I don't care about that.
304- */
305- IF @is_system_health = 1
306- AND
307- (
308- LOWER (@target_type) = N ' table'
309- OR @log_to_table = 1
310- )
311- BEGIN
312- RAISERROR (' Logging system_health to a table is not supported.
313- Either pick a different session or change both
314- @target_type to be '' event_file'' or '' ring_buffer''
315- and @log_to_table to be 0.' , 11 , 0 ) WITH NOWAIT ;
316- RETURN ;
317- END
318-
319- IF @is_system_health = 1
320- AND @target_type IS NULL
321- BEGIN
322- RAISERROR (' No @target_type specified, using '' event_file'' for system_health.' , 0 , 1 ) WITH NOWAIT ;
323- SELECT
324- @target_type = ' event_file' ;
325- END
326-
327268/* Check if the blocked process report is on at all*/
328269IF EXISTS
329270(
@@ -332,8 +273,8 @@ IF EXISTS
332273 FROM sys .configurations AS c
333274 WHERE c .name = N ' blocked process threshold (s)'
334275 AND CONVERT (int , c .value_in_use ) = 0
335- AND @is_system_health = 0
336276)
277+ AND @session_name NOT LIKE N ' system%health'
337278BEGIN
338279 RAISERROR (N' Unless you want to use the lousy version in system_health, the blocked process report needs to be enabled:
339280EXECUTE sys.sp_configure '' show advanced options'' , 1;
@@ -351,8 +292,8 @@ IF EXISTS
351292 FROM sys .configurations AS c
352293 WHERE c .name = N ' blocked process threshold (s)'
353294 AND CONVERT (int , c .value_in_use ) <> 5
354- AND @is_system_health = 0
355295)
296+ AND @session_name NOT LIKE N ' system%health'
356297BEGIN
357298 RAISERROR (N' For best results, set up the blocked process report like this:
358299EXECUTE sys.sp_configure '' show advanced options'' , 1;
@@ -381,6 +322,8 @@ DECLARE
381322 @session_id integer ,
382323 @target_session_id integer ,
383324 @file_name nvarchar (4000 ),
325+ @is_system_health bit = 0 ,
326+ @is_system_health_msg nchar (1 ),
384327 @inputbuf_bom nvarchar (1 ) =
385328 CONVERT (nvarchar (1 ), 0x0a00, 0 ),
386329 @start_date_original datetime2 = @start_date,
@@ -465,6 +408,12 @@ SELECT
465408 @end_date
466409 )
467410 END ,
411+ @is_system_health =
412+ CASE
413+ WHEN @session_name LIKE N ' system%health'
414+ THEN 1
415+ ELSE 0
416+ END ,
468417 @mdsql = N'
469418IF OBJECT_ID('' {table_check}'' , '' U'' ) IS NOT NULL
470419BEGIN
@@ -493,9 +442,52 @@ BEGIN
493442 FROM {table_check};
494443END;' ;
495444
445+ IF @debug = 1
446+ AND @is_system_health = 0
447+ BEGIN
448+ RAISERROR (' We are not using system_health' , 0 , 1 ) WITH NOWAIT ;
449+ END ;
450+
451+ IF @is_system_health = 1
452+ BEGIN
453+ RAISERROR (' For best results, consider not using system_health as your target. Re-run with @help = 1 for guidance.' , 0 , 1 ) WITH NOWAIT ;
454+ END
455+
456+ /*
457+ Note: I do not allow logging to a table from system_health, because the set of columns
458+ and available data is too incomplete, and I don't want to juggle multiple
459+ table definitions.
460+
461+ Logging to a table is only allowed from a blocked_process_report Extended Event,
462+ but it can either be ring buffer or file target. I don't care about that.
463+ */
464+ IF @is_system_health = 1
465+ AND
466+ (
467+ LOWER (@target_type) = N ' table'
468+ OR @log_to_table = 1
469+ )
470+ BEGIN
471+ RAISERROR (' Logging system_health to a table is not supported.
472+ Either pick a different session or change both
473+ @target_type to be '' event_file'' or '' ring_buffer''
474+ and @log_to_table to be 0.' , 11 , 0 ) WITH NOWAIT ;
475+ RETURN ;
476+ END
477+
478+ IF @is_system_health = 1
479+ AND @target_type IS NULL
480+ BEGIN
481+ RAISERROR (' No @target_type specified, using the '' event_file'' for system_health.' , 0 , 1 ) WITH NOWAIT ;
482+ SELECT
483+ @target_type = ' event_file' ;
484+ END
485+
496486SELECT
497487 @azure_msg =
498- CONVERT (nchar (1 ), @azure);
488+ CONVERT (nchar (1 ), @azure),
489+ @is_system_health_msg =
490+ CONVERT (nchar (1 ), @is_system_health);
499491
500492/* Change this here in case someone leave it NULL*/
501493IF ISNULL (@target_database, DB_NAME ()) IS NOT NULL
0 commit comments