... 2000覯 襷..
れ 襷れ伎 ...

IF object_id('usp_batch_window') IS NOT NULL
	DROP proc usp_batch_window
go
 
CREATE proc usp_batch_window
	@unit int = 10 --覿 
,	@OPTION tinyint = 0 --0:豌, 1:螻蠏碁9
,	@time char(5) = NULL
 
AS
/*
: 
	--殊  豌企慨蠍
	exec usp_batch_window 15, 0
 
	--殊  30覿襦 豌企慨蠍
	exec usp_batch_window 30, 0
 
	--殊 蠏碁9 襦 覲願鍵
	exec usp_batch_window 15, 1
 
	--轟螳 ろ 覲願鍵
	exec usp_batch_window 15, 0, '06:35'
 
	--企覃 
	exec usp_batch_window -1, 0, '06:35'
 
*/
 
IF @unit <= 0 
begin
	SELECT 
		case 
			when @unit = 0 then '企 覃豢! 0朱 蠍 る! 1伎 螳 ロ!'
			else '襷企(-)襯 ロ覃 覲願 伎手~'
		end 覃讌
	RETURN
end
 
SELECT
	name
,	job_id
,	step_name
,	job_name 
,	step_id 
,	exec_begin_time 
,	max_run_duration
INTO #temp
FROM (
	SELECT
		min(job_name) job_name
	,	job_id
	,	min(name) name
	,	min(step_name) step_name
	,	min(step_id) step_id
	,	min(exec_begin_time) exec_begin_time
	,	max(run_duration) max_run_duration
	FROM (
		SELECT 
			a.name + '(' + b.step_name + ')' job_name
		,	a.name
		,	a.job_id
		,	b.step_name
		,	b.step_id 
		,	LEFT(RIGHT('000000' + cast(c.next_run_time AS varchar(8)), 6),2) + ':' +
			substring(RIGHT('000000' + cast(c.next_run_time AS varchar(8)), 6), 3, 2) exec_begin_time
		,	b.run_duration
		FROM msdb..sysjobs a INNER JOIN msdb..sysjobhistory b
		ON a.job_id = b.job_id INNER JOIN msdb..sysjobschedules c
		ON a.job_id = c.job_id 
		WHERE b.step_id > 0
		AND a.enabled = 1
		--2005伎 覯  譟郁唄 企豪 貉殊 .
		--AND c.freq_type = 4
		--AND c.freq_subday_interval = 0
	) t
	GROUP BY 
		job_id
	,	job_name
	,	step_id
	,	exec_begin_time
) t
 
--金撃もモр
--declare @unit tinyint
--set @unit = 1
 
SELECT
	name 覈
,	step_name 螻覈
,	step_id 螻
,	exec_begin_time ろ螳
,	exec_end_time ろ譬襭螳
,	replicate('', datediff(mi, case when @OPTION = 0 then '00:00' else begin_time end, exec_begin_time) / @unit) +
	replicate('', case when datediff(mi, exec_begin_time, exec_end_time) / @unit = 0 then 1 else datediff(mi, exec_begin_time, exec_end_time) / @unit end) + 
	'(' + cast(sec_run_duration AS varchar(6)) + ')' 谿
FROM (
	SELECT
		name
	,	step_name
	--,	job_name 
	,	step_id
	--,	exec_begin_time
	,	begin_time
	,	RIGHT(convert(char(16), dateadd(ss, acc_run_duration - max_run_duration, exec_begin_time + ':00'), 121), 5) exec_begin_time
	,	RIGHT(convert(char(16), dateadd(ss, acc_run_duration, exec_begin_time + ':00'), 121), 5) exec_end_time
	,	case when acc_run_duration/60 < 60 then 1 else acc_run_duration/60 end acc_run_duration
	,	case when max_run_duration/60 < 60 then 1 else max_run_duration/60 end min_run_duration
	,	max_run_duration sec_run_duration
	FROM (
		SELECT
			name
		,	step_name
		,	job_name 
		,	step_id
		,	exec_begin_time
		,	(SELECT min(exec_begin_time) FROM #temp where a.job_id = job_id) begin_time
		,	max_run_duration
		,	(SELECT sum(max_run_duration) FROM #temp where a.job_id = job_id and a.step_id >= step_id) acc_run_duration
		FROM #temp a
	) t
) t
WHERE case when @time IS NULL then '00:00' else @time end BETWEEN 
		case when @time IS NULL then '00:00' else exec_begin_time end AND 
		case when @time IS NULL then '11:59' else exec_end_time end
ORDER BY 
	case when @OPTION = 0 then exec_begin_time else name end
,	case when @OPTION = 1 then step_id end
go