Monthly Archives: December 2015

Check if a sql server scheduled job will be running at a specific day

USE msdb
;WITH CTE AS (SELECT schedule_id, job_id, RIGHT(‘0’+CAST(next_run_time AS VARCHAR(6)),6) AS next_run_time, next_run_date
FROM sysjobschedules)
SELECT A.name Job_Name,
‘Will be running at ‘+
SUBSTRING(CONVERT(VARCHAR(10), CASE WHEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) > 12
THEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time),1,2) -12
ELSE SUBSTRING (CONVERT(VARCHAR(10),next_run_time),1,2) END),1,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time),3,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),5,2) ‘Scheduled At’
FROM sysjobs A ,CTE B
WHERE A.job_id = B.job_id
AND SUBSTRING(CONVERT(VARCHAR(10),next_run_date) , 5,2) +’/’+
SUBSTRING(CONVERT(VARCHAR(10),next_run_date) , 7,2) +’/’+
SUBSTRING(CONVERT(VARCHAR(10),next_run_date),1,4) = CONVERT(VARCHAR(10), cast( ‘2016/01/01′ as date),101)
AND (SUBSTRING( CONVERT(VARCHAR(10),
CASE WHEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) > 12
THEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) -12
ELSE SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) END),1,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),3,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),5,2)) >
SUBSTRING (CONVERT( VARCHAR(30) , cast( ‘2016/01/01’ as date),9),13,7)

how to check next run time for a sql server job

— All Jobs information are stored in msdb
USE msdb
;WITH CTE AS (SELECT schedule_id, job_id, RIGHT(‘0’+CAST(next_run_time AS VARCHAR(6)),6) AS next_run_time, next_run_date
FROM sysjobschedules)
SELECT A.name Job_Name,
‘Will be running at ‘+
SUBSTRING(CONVERT(VARCHAR(10), CASE WHEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) > 12
THEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time),1,2) -12
ELSE SUBSTRING (CONVERT(VARCHAR(10),next_run_time),1,2) END),1,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time),3,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),5,2) ‘Scheduled At’
FROM sysjobs A ,CTE B
WHERE A.job_id = B.job_id
AND SUBSTRING(CONVERT(VARCHAR(10),next_run_date) , 5,2) +’/’+
SUBSTRING(CONVERT(VARCHAR(10),next_run_date) , 7,2) +’/’+
SUBSTRING(CONVERT(VARCHAR(10),next_run_date),1,4) = CONVERT(VARCHAR(10),GETDATE(),101)
AND (SUBSTRING( CONVERT(VARCHAR(10),
CASE WHEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) > 12
THEN SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) -12
ELSE SUBSTRING (CONVERT(VARCHAR(10),next_run_time) , 1 ,2) END),1,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),3,2)
+’:’+SUBSTRING (CONVERT(VARCHAR(10), next_run_time ),5,2)) >
SUBSTRING (CONVERT( VARCHAR(30) , GETDATE(),9),13,7)

Changing MSDB to point to new file

find the file you need to move
C:\Program Files\Microsoft SQL Server\MSSQL12(this can be different)\MSSQL\DATA

copy the file MSDBData.mdf (rename it to to MSDBData_test.mdf )your destination (don’t overwrite your msdb file on your test server)

go to your test db server

create a new testdb, (you won’t be able to detach msdb)

run detach command to detach your testdb

sp_detach_db @dbname= ‘testdb’

now let the test db attached to the MSDB file you copied.

–path has to be correct in the following
USE [master]
GO
— Method 1: I use this method
EXEC sp_attach_single_file_db @dbname=’TestDb’,
@physname=N’C:\temp\MSDBData_temp.mdf’
GO

now your testdb is pointing to the new MSDB file, which means you testdb is the msdb

run the following queries.

select * From sysjobs — this will have all the job names in the table

select table_name from INFORMATION_SCHEMA.tables where TABLE_NAME like ‘%job%’;