What's the values for the Freq_Interval column in MSDB.dbo.SysSchedules when Freq_Type is weekly and more than one day is selected in the schedule?

dba.stackexchange https://dba.stackexchange.com/questions/231402

سؤال

I'm reading up on the SysSchedules table in the Microsoft API, and am trying to incorporate using this table in a query.

In the documentation, the Freq_Interval column seems to be defined out for when a single day is selected in the schedule and the Freq_Type is weekly. But what are the values for every other combination of days when more than one day is selected?

For example, I have a job which is scheduled to occur weekly on the days Monday, Tuesday, Wednesday, Thursday, and Friday. I see in the MSDB.dbo.SysSchedules table, it has a Freq_Interval of 62 (which isn't listed in the documentation.)

Job Schedule Details MSDB.dbo.SysSchedules

هل كانت مفيدة؟

المحلول

The documentation states

freq_interval is one or more of the following:

1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday

These values are all powers of 2.

When you select multiple days simply add the distinct codes.

2 + 4 + 8 + 16 + 32 = 62

or alternatively use the bitwise or operator to combine them.

2 | 4 | 8 | 16 | 32

(my recommendation would be to use | in code and + if adding them up in your head! - | is the "correct" thing to use and won't behave incorrectly if you have duplicate values in the list)

If you are trying to do the reverse operation you need bitwise and

DECLARE @Sunday TINYINT = 1,
        @Monday TINYINT = 2,
        @Tuesday TINYINT = 4,
        @Wednesday TINYINT = 8,
        @Thursday TINYINT = 16,
        @Friday TINYINT = 32,
        @Saturday TINYINT = 64;

DECLARE @TestValue TINYINT = 62;

SELECT CASE WHEN @TestValue & @Sunday = @Sunday THEN 'Y' ELSE 'N' END AS Sunday,
       CASE WHEN @TestValue & @Monday = @Monday THEN 'Y' ELSE 'N' END AS Monday,
       CASE WHEN @TestValue & @Tuesday = @Tuesday THEN 'Y' ELSE 'N' END AS Tuesday,
       CASE WHEN @TestValue & @Wednesday = @Wednesday THEN 'Y' ELSE 'N' END AS Wednesday,
       CASE WHEN @TestValue & @Thursday = @Thursday THEN 'Y' ELSE 'N' END AS Thursday,
       CASE WHEN @TestValue & @Friday = @Friday THEN 'Y' ELSE 'N' END AS Friday,
       CASE WHEN @TestValue & @Saturday = @Saturday THEN 'Y' ELSE 'N' END AS Saturday
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى dba.stackexchange
scroll top