<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <Body>
    <ReportItems>
      <Textbox Name="Textbox1">
        <CanGrow>true</CanGrow>
        <KeepTogether>true</KeepTogether>
        <Paragraphs>
          <Paragraph>
            <TextRuns>
              <TextRun>
                <Value>Create Baseline Calculation Job</Value>
                <Style>
                  <FontSize>20pt</FontSize>
                </Style>
              </TextRun>
            </TextRuns>
            <Style>
              <TextAlign>Center</TextAlign>
            </Style>
          </Paragraph>
        </Paragraphs>
        <rd:DefaultName>Textbox1</rd:DefaultName>
        <Top>0.27167in</Top>
        <Left>0.38in</Left>
        <Height>0.51667in</Height>
        <Width>7.375in</Width>
        <Style>
          <Border>
            <Style>None</Style>
          </Border>
          <BackgroundColor>LightSteelBlue</BackgroundColor>
          <VerticalAlign>Middle</VerticalAlign>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
      </Textbox>
      <Textbox Name="Textbox2">
        <CanGrow>true</CanGrow>
        <KeepTogether>true</KeepTogether>
        <Paragraphs>
          <Paragraph>
            <TextRuns>
              <TextRun>
                <Value>USE [msdb]


/****** Object:  Job [MonitorCalculateBaseline]    Script Date: 5/9/2023 7:45:00 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 5/9/2023 7:45:00 PM ******/


DECLARE @jobId BINARY(16)
select @jobId = job_id from msdb.dbo.sysjobs where (name = N'MonitorCalculateBaseline')
if (@jobId is NULL)
BEGIN
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'MonitorCalculateBaseline', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'No description available.', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback

END
/****** Object:  Step [BlockingBaseline]    Script Date: 5/9/2023 7:45:01 PM ******/
IF NOT EXISTS (SELECT * FROM msdb.dbo.sysjobsteps WHERE job_id = @jobId and step_id = 1)
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'BlockingBaseline', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@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'DECLARE @NumDays INT
DECLARE @MinCaptures INT
DECLARE @ActualCaptures INT
DECLARE @BaseLineValue INT

SELECT @NumDays = [ConfigValue]
FROM AdminMonitor.dbo.Config
WHERE ConfigTitle = ''NumberOfDaysForBaseline''

SELECT @MinCaptures = [ConfigValue]
FROM AdminMonitor.dbo.Config
WHERE ConfigTitle = ''MinCapturesBaseline''

SET @Numdays = @Numdays * -1

Select @ActualCaptures = COUNT([NumberOfEvents])
FROM AdminMonitor.[dbo].[BlockingCount]
WHERE [DateOfCheck] &gt; DATEADD(day, @NumDays, GETDATE())

IF @ActualCaptures &lt; @MinCaptures
	BEGIN
		UPDATE AdminMonitor.[dbo].[Baseline]
			SET [BaselineValue] = 0
				,[DateOfBaseLine] = Getdate()
				, [NumberOfCapturesUse] = @ActualCaptures
			WHERE CounterName = ''BlockingCount''
	END
ELSE
	BEGIN
		SELECT @BaseLineValue = AVG([NumberOfEvents])
		FROM AdminMonitor.[dbo].[BlockingCount]
		WHERE [DateOfCheck] &gt; DATEADD(day, @NumDays, GETDATE())
		UPDATE AdminMonitor.[dbo].[Baseline]
			SET [BaselineValue] = @BaseLineValue
				,[DateOfBaseLine] = Getdate()
				, [NumberOfCapturesUse] = @ActualCaptures
			WHERE CounterName = ''BlockingCount''
	END', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
/****** Object:  Step [DeadlockCountBaseline]    Script Date: 5/9/2023 7:45:01 PM ******/
IF NOT EXISTS (SELECT * FROM msdb.dbo.sysjobsteps WHERE job_id = @jobId and step_id = 2)
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'DeadlockCountBaseline', 
		@step_id=2, 
		@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'

DECLARE @NumDays INT
DECLARE @MinCaptures INT
DECLARE @ActualCaptures INT
DECLARE @BaseLineValue INT

SELECT @NumDays = [ConfigValue]
FROM AdminMonitor.dbo.Config
WHERE ConfigTitle = ''NumberOfDaysForBaseline''

SELECT @MinCaptures = [ConfigValue]
FROM AdminMonitor.dbo.Config
WHERE ConfigTitle = ''Deadlock''

SET @Numdays = @Numdays * -1

Select @ActualCaptures = COUNT([NumberOfEvents])
FROM AdminMonitor.[dbo].DeadlockCount
WHERE [DateOfCheck] &gt; DATEADD(day, @NumDays, GETDATE())

IF @ActualCaptures &lt; @MinCaptures
	BEGIN
		UPDATE AdminMonitor.[dbo].[Baseline]
			SET [BaselineValue] = 0
				,[DateOfBaseLine] = Getdate()
				, [NumberOfCapturesUse] = @ActualCaptures
			WHERE CounterName = ''Deadlock''
	END
ELSE
	BEGIN
		SELECT @BaseLineValue = AVG([NumberOfEvents])
		FROM AdminMonitor.[dbo].[DeadlockCount]
		WHERE [DateOfCheck] &gt; DATEADD(day, @NumDays, GETDATE())

		UPDATE AdminMonitor.[dbo].[Baseline]
			SET [BaselineValue] = @BaseLineValue
				,[DateOfBaseLine] = Getdate()
				, [NumberOfCapturesUse] = @ActualCaptures
			WHERE CounterName = ''Deadlock''
	END', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Monthly', 
		@enabled=1, 
		@freq_type=16, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20230502, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT &gt; 0) ROLLBACK TRANSACTION
EndSave:</Value>
                <Style />
              </TextRun>
            </TextRuns>
            <Style />
          </Paragraph>
        </Paragraphs>
        <rd:DefaultName>Textbox2</rd:DefaultName>
        <Top>2.355in</Top>
        <Left>0.38in</Left>
        <Height>29.325in</Height>
        <Width>7.375in</Width>
        <ZIndex>1</ZIndex>
        <Style>
          <Border>
            <Style>None</Style>
          </Border>
          <BackgroundColor>LightGrey</BackgroundColor>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
      </Textbox>
      <Textbox Name="Textbox3">
        <CanGrow>true</CanGrow>
        <KeepTogether>true</KeepTogether>
        <Paragraphs>
          <Paragraph>
            <TextRuns>
              <TextRun>
                <Value>This code create the job to capture the wait counters.  Export this report to Word to copy and paste.</Value>
                <Style>
                  <FontSize>14pt</FontSize>
                </Style>
              </TextRun>
            </TextRuns>
            <Style>
              <TextAlign>Center</TextAlign>
            </Style>
          </Paragraph>
        </Paragraphs>
        <rd:DefaultName>Textbox3</rd:DefaultName>
        <Top>0.9925in</Top>
        <Left>0.38in</Left>
        <Height>1.08195in</Height>
        <Width>3.30833in</Width>
        <ZIndex>2</ZIndex>
        <Style>
          <Border>
            <Style>None</Style>
          </Border>
          <BackgroundColor>LightGrey</BackgroundColor>
          <VerticalAlign>Middle</VerticalAlign>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
      </Textbox>
      <Textbox Name="Textbox4">
        <CanGrow>true</CanGrow>
        <KeepTogether>true</KeepTogether>
        <Paragraphs>
          <Paragraph>
            <TextRuns>
              <TextRun>
                <Value>Back to Previous Page</Value>
                <Style>
                  <FontSize>14pt</FontSize>
                </Style>
              </TextRun>
            </TextRuns>
            <Style>
              <TextAlign>Center</TextAlign>
            </Style>
          </Paragraph>
        </Paragraphs>
        <rd:DefaultName>Textbox4</rd:DefaultName>
        <ActionInfo>
          <Actions>
            <Action>
              <Drillthrough>
                <ReportName>MonitorCreateJobsCode</ReportName>
              </Drillthrough>
            </Action>
          </Actions>
        </ActionInfo>
        <Top>0.9925in</Top>
        <Left>3.92167in</Left>
        <Height>1.08195in</Height>
        <Width>3.83333in</Width>
        <ZIndex>3</ZIndex>
        <Style>
          <Border>
            <Style>None</Style>
          </Border>
          <BackgroundColor>LightSteelBlue</BackgroundColor>
          <VerticalAlign>Middle</VerticalAlign>
          <PaddingLeft>2pt</PaddingLeft>
          <PaddingRight>2pt</PaddingRight>
          <PaddingTop>2pt</PaddingTop>
          <PaddingBottom>2pt</PaddingBottom>
        </Style>
      </Textbox>
    </ReportItems>
    <Height>32.61333in</Height>
    <Style>
      <Border>
        <Style>None</Style>
      </Border>
      <BackgroundColor>DarkBlue</BackgroundColor>
    </Style>
  </Body>
  <Width>8.23333in</Width>
  <Page>
    <PageHeight>455in</PageHeight>
    <LeftMargin>1in</LeftMargin>
    <RightMargin>1in</RightMargin>
    <TopMargin>1in</TopMargin>
    <BottomMargin>1in</BottomMargin>
    <Style />
  </Page>
  <AutoRefresh>0</AutoRefresh>
  <rd:ReportUnitType>Inch</rd:ReportUnitType>
  <rd:ReportID>97e2571f-f3b8-4c8a-a03a-32c9f45f3a3e</rd:ReportID>
</Report>