sql: daily check

这是一个用于执行日常检查的SQL脚本,包括手动检查和SQL查询模式。脚本中定义了一个临时过程 DailyCheckPrint,用于打印检查信息。初始化部分声明了变量,然后打印一般信息,接着进行具体的检查项。添加、修改或删除检查项都有详细的步骤说明。测试时,建议每次修改后先单独测试新项,确保无误后再整合到主脚本。
摘要由CSDN通过智能技术生成

Script

----This is a temporary procedure, used to print information in Messages box-----------
----In each session, it could only be executed one time
create procedure #DailyCheckPrint
	@Casecnt int,
	@DailyCheck_title nvarchar(100),
	@DailyCheck_Mode nvarchar(100),
	@OperationSteps nvarchar(4000),
	@CheckResult nvarchar(100),
	@ResultCnt nvarchar(10) = 0,
	@PassFlag int = 0
as
begin
	print '---------------------------------------------------------------------------------------'
	set @DailyCheck_title = ' Daily Check '+ substring(convert(nvarchar(5),(@Casecnt)),2,4) +' : ' + @DailyCheck_title
	print @DailyCheck_title

	print '     Check Mode : ' + @DailyCheck_Mode
		
if @DailyCheck_Mode = 'Manual'
	begin
		set @OperationSteps = 'Operation Steps : ' + @OperationSteps
		print @OperationSteps

		set @CheckResult = '   Check Result : (PASS or your comment)'
		print @CheckResult
	end
else if @DailyCheck_Mode = 'SQL'
	begin							 
		print '   Result Count : ' + @ResultCnt

		if @PassFlag = 1 
			print '   Check Result : PASS'
		else
			begin
				print '   Check Result : Not Pass, Need further operations'

				set @OperationSteps = 'Operation Steps : ' + @OperationSteps
				print @OperationSteps
			end 
		print 'Further comment : null'
	end
end
go


---------------- daily check initialization ----------------
declare @DailyCheck_title nvarchar(300)
declare @DailyCheck_Mode nvarchar(100)
declare @ResultCnt nvarchar(6) = 0
declare @DateTime datetime
declare @ExecuteSQL nvarchar(4000)
declare @OperationSteps nvarchar(4000)
declare @Executor nvarchar(20) 
declare @CheckResult nvarchar(100) = '   Check Result : (PASS or your comment)'
declare @Casecnt int = 100
declare @PassFlag int = 0


---------------- print general information ----------------
select @Executor = SESSION_USER
select @DateTime = getdate()
print convert(varchar(30),getdate(),114) + ': Today is ' + convert(varchar(30),@DateTime,104)
print convert(varchar(30),getdate(),114) + ': The daily check is done by ' + @Executor
print convert(varchar(30),getdate(),114) + ': Now daily check script starts'


---------------- daily check items -----------------------

----------------------------------------------------------------------------------------------------
set @DailyCheck_title = 'Manual-SIAM'							----add or modify the title
set @DailyCheck_Mode  = 'Manual'						----add or modify the mode, it could only use "SQL" or "Manual"
set @OperationSteps   = 'Check the SIAM Incident Queue for High prio tasks
				See if any prio 1 or prio 2 incidents are in our queue'	----add simple operation steps

--Do not modify this command-------------
set @Casecnt += 1
exec #DailyCheckPrint @Casecnt, @DailyCheck_title, @DailyCheck_Mode, @OperationSteps, @CheckResult


----------------------------------------------------------------------------------------------------
set @DailyCheck_title = 'PoolMBDuplicateApproverOrOwner'	----add or modify the title
set @DailyCheck_Mode  = 'SQL'				----Don't change it

select @ResultCnt = count(1) from (
-------------------------------------------start of checking sql 
SELECT UID_JOB,BasisObjectKey FROM JobQueue WHERE
ready2exe = 'frozen'
AND (JobChainName = 'MR_PoolMailbox_Insert' or JobChainName = 'MR_DistributionGroup_Insert')
AND ErrorMessages like '%Error during execution of statement: insert into PersonInOrg%'

-------------------------------------------end of checking sql 
) cmd

-----------judge the executing result
if (@ResultCnt = 0)  
	set @PassFlag = 1 
else
begin
	set @PassFlag = 0
	set @OperationSteps = 'Please check the workaround from TA841839'
end
-----------Below no need to modify 
set @Casecnt += 1
exec #DailyCheckPrint @Casecnt, @DailyCheck_title, @DailyCheck_Mode, @OperationSteps, @CheckResult, @ResultCnt, @PassFlag

Readme

INTRODUCTION

The script is used to assist to do the daily check, consisting of 4 parts.
The first part is temporary procedure #DailyCheckPrint, which is used to print daily check information and sql check result.
The second part is ‘daily check initialization’, which is used to declare the parameters.
The third part is ‘print general information’, which is used to print some general information
The fourth part is ‘daily check items’, which contains the all the checking items.

EXECUTE

Open SQL Server Management Studio, and choose the prod database server, select the iamdb, create one query page.
Copy all the daily check script to SQL Server Management Studio query page.
Click Execute button to run the script, simply check the output messages, if there is something in red.
Copy the Message result to one notepad and save it to a txt file.
Follow the message content to do the daily check, You can add your checking result in this file.

ADD

There are now 2 template modes, manual mode and SQL mode.

Manual template

If you are adding one manual daily check item, please copy the template to the script, change the value of DailyCheck_title, OperationSteps

SQL template

If you are adding one SQL daily check item, please copy the template to the script, change the value of DailyCheck_title, add the check sql, change the value of OperationSteps

Attention

  1. if the checking sql have “Order by”, please comment it.
  2. If there are single quotation marks in OperationSteps, it would cause ambiguity. replace one single quatation mark to two marks.
    e.g. CHANGE ready2exe=‘frozen’ To ready2exe=’‘frozen’’
  3. Before adding the new item script to the general script, please don’t forget to test it.

MODIFY

You can follow the steps of {ADD}

DELETE

If you want to delete one daily check, just comment or delete the item from line “set @DailyCheck_title” to line “exec #DailyCheckPrint”.

TEST

It is suggested that each modification of daily check script should be tested.

Test steps

  • Open a new session window in SQL Server Management Studio, select correct database.
  • Copy the first part of the script to the query window, execute it, delete it. (Script parts is introduced in the head of this document)
  • Copy the second part of the script to the query window.
  • Copy your daily check item code to the query window, execute all the script
  • Check the execution result. If the execution reuslt is fine, you can add your code to the main script. If there is something wrong, check and fix it.

Template

Manual template

----Manual template
--If you are now adding one Manual daily check, please copy the template to the script and only change the value of DailyCheck_title, OperationSteps
----------------------------------------------------------------------------------------------------
set @DailyCheck_title = ''							----add or modify the title
set @DailyCheck_Mode  = 'Manual'						----Don't change it
set @OperationSteps   = 'Please refer operation manual'	----add simple operation steps

--Do not modify this command-------------
set @Casecnt += 1
exec #DailyCheckPrint @Casecnt, @DailyCheck_title, @DailyCheck_Mode, @OperationSteps, @CheckResult

SQL template

----SQL template
--If you are now adding one SQL daily check, please copy the template to the script, 
--change the value of DailyCheck_title, add the check sql, change the value of OperationSteps

--Attention:
--1. if the checking sql have "Order by", please comment it.
--2. If there are single quotation marks in OperationSteps, it would cause ambiguity. replace one single quatation mark to two marks.
---- e.g. CHANGE ready2exe='frozen' To ready2exe=''frozen''
--3. Before adding the new item script to the general script, please don't forget to test it.

----------------------------------------------------------------------------------------------------
set @DailyCheck_title = ''					----add or modify the title
set @DailyCheck_Mode  = 'SQL'				----Don't change it

select @ResultCnt = count(1) from (
-------------------------------------------start of checking sql 

-------------------------------------------end of checking sql 
) cmd

-----------judge the executing result
if (@ResultCnt = 0)  
	set @PassFlag = 1 
else
begin
	set @PassFlag = 0
	set @OperationSteps = 'Please check operations document to implement'
end
-----------Below no need to modify 
set @Casecnt += 1
exec #DailyCheckPrint @Casecnt, @DailyCheck_title, @DailyCheck_Mode, @OperationSteps, @CheckResult, @ResultCnt, @PassFlag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值