/********************************************************************************************
 Section 1: Daily Job Schedule Lookup
 --------------------------------------------------------------------------------------------
 Description:
   Returns all SQL Agent jobs scheduled to run on a given date, along with their control
   status (IsActive) and configured runtime from JobControl.

 Parameters:
   @CheckDate - The date to check job schedules for.

 Notes:
   • Uses MSDB’s internal schedule definitions (sysschedules, sysjobschedules).
   • Supports One-time, Daily, Weekly, and Monthly schedule types.
   • Can easily be adapted for DEVL / TEST / PROD by changing @DbName.

********************************************************************************************/
USE msdb;
GO

DECLARE @CheckDate DATE = CAST(GETDATE() AS DATE);  -- Change if needed
DECLARE @CheckWeekday INT = DATEPART(WEEKDAY, @CheckDate);
DECLARE @Env SYSNAME = 'DEVL';                     -- Change for TEST or PROD
DECLARE @DbName SYSNAME;

-- Map environment to the right control DB
SET @DbName = CASE @Env
                 WHEN 'DEVL' THEN 'DevlDUTASJobSchedule'
                 WHEN 'TEST' THEN 'TestDUTASJobSchedule'
                 WHEN 'PROD' THEN 'ProdDUTASJobSchedule'
              END;

DECLARE @Sql NVARCHAR(MAX);
SET @Sql = N'
SELECT 
    j.name AS SQLAgentJobName,
    jc.JobName AS ControlJobName,
    jc.ScheduledStartTime,
    jc.IsActive,
    s.name AS ScheduleName,
    s.freq_type,
    s.freq_interval,
    s.freq_subday_type,
    s.freq_subday_interval,
    s.active_start_date,
    CASE s.freq_type
        WHEN 1 THEN ''One-time''
        WHEN 4 THEN ''Daily''
        WHEN 8 THEN ''Weekly''
        WHEN 16 THEN ''Monthly''
        ELSE ''Other''
    END AS FrequencyType,
    CASE 
        WHEN s.freq_type = 1 
             AND s.active_start_date = CONVERT(INT, CONVERT(CHAR(8), @CheckDateParam, 112))
             THEN 1
        WHEN s.freq_type = 4 
             AND s.active_start_date <= CONVERT(INT, CONVERT(CHAR(8), @CheckDateParam, 112))
             THEN 1
        WHEN s.freq_type = 8  -- Weekly
             AND (POWER(2, @CheckWeekdayParam - 1) & s.freq_interval) > 0
             THEN 1
        WHEN s.freq_type = 16 -- Monthly
             AND DAY(@CheckDateParam) = s.freq_interval
             THEN 1
        ELSE 0
    END AS IsScheduledToday
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobschedules js ON j.job_id = js.job_id
JOIN msdb.dbo.sysschedules s ON js.schedule_id = s.schedule_id
LEFT JOIN ' + QUOTENAME(@DbName) + '.dbo.JobControl jc ON jc.JobName = j.name
WHERE jc.IsActive = 1
  AND (
        (s.freq_type = 1 AND s.active_start_date = CONVERT(INT, CONVERT(CHAR(8), @CheckDateParam, 112)))  -- One-time
     OR (s.freq_type = 4 AND s.active_start_date <= CONVERT(INT, CONVERT(CHAR(8), @CheckDateParam, 112))) -- Daily
     OR (s.freq_type = 8 AND (POWER(2, @CheckWeekdayParam - 1) & s.freq_interval) > 0)                    -- Weekly
     OR (s.freq_type = 16 AND DAY(@CheckDateParam) = s.freq_interval)                                     -- Monthly
      )
ORDER BY jc.ScheduledStartTime;
';

EXEC sp_executesql 
    @Sql,
    N'@CheckDateParam DATE, @CheckWeekdayParam INT',
    @CheckDateParam = @CheckDate,
    @CheckWeekdayParam = @CheckWeekday;
GO
