我被迫使用具有以下结构 (ScheduledEmails) 的表,并希望找到处理多个变量设置为 1 (true) 的最有效方法
如果只选择了一个,下面的脚本只会返回正确的值。
任何人都可以推荐一种修改底部查询以适应多个条件的方法。
例如@IsMonday 和@IsTuesday 在哪里是真的等等?
我尝试了多种 AND/OR 组合,但似乎都没有产生预期的结果。
DECLARE @IsMonday bit
DECLARE @IsTuesday bit
DECLARE @IsWednesday bit
DECLARE @IsThursday bit
DECLARE @IsFriday bit
DECLARE @IsSaturday bit
DECLARE @IsSunday bit
DECLARE @Is0000 bit
DECLARE @Is0100 bit
DECLARE @Is0200 bit
DECLARE @Is0300 bit
DECLARE @Is0400 bit
DECLARE @Is0500 bit
DECLARE @Is0600 bit
DECLARE @Is0700 bit
DECLARE @Is0800 bit
DECLARE @Is0900 bit
DECLARE @Is1000 bit
DECLARE @Is1100 bit
DECLARE @Is1200 bit
DECLARE @Is1300 bit
DECLARE @Is1400 bit
DECLARE @Is1500 bit
DECLARE @Is1600 bit
DECLARE @Is1700 bit
DECLARE @Is1800 bit
DECLARE @Is1900 bit
DECLARE @Is2000 bit
DECLARE @Is2100 bit
DECLARE @Is2200 bit
DECLARE @Is2300 bit
DECLARE @dayOfWeek VARCHAR(20)
DECLARE @hourOfDay int
SET @dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET @hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET @IsMonday = CASE WHEN @dayOfWeek = 'Monday' THEN 1 ELSE 0 END
SET @IsTuesday = CASE WHEN @dayOfWeek = 'Tuesday' THEN 1 ELSE 0 END
SET @IsWednesday = CASE WHEN @dayOfWeek = 'Wednesday' THEN 1 ELSE 0 END
SET @IsThursday = CASE WHEN @dayOfWeek = 'Thursday' THEN 1 ELSE 0 END
SET @IsFriday = CASE WHEN @dayOfWeek = 'Friday' THEN 1 ELSE 0 END
SET @IsSaturday = CASE WHEN @dayOfWeek = 'Saturday' THEN 1 ELSE 0 END
SET @IsSunday = CASE WHEN @dayOfWeek = 'Sunday' THEN 1 ELSE 0 END
SET @Is0000 = CASE WHEN @hourOfDay = 0 THEN 1 ELSE 0 END
SET @Is0100 = CASE WHEN @hourOfDay = 1 THEN 1 ELSE 0 END
SET @Is0200 = CASE WHEN @hourOfDay = 2 THEN 1 ELSE 0 END
SET @Is0300 = CASE WHEN @hourOfDay = 3 THEN 1 ELSE 0 END
SET @Is0400 = CASE WHEN @hourOfDay = 4 THEN 1 ELSE 0 END
SET @Is0500 = CASE WHEN @hourOfDay = 5 THEN 1 ELSE 0 END
SET @Is0600 = CASE WHEN @hourOfDay = 6 THEN 1 ELSE 0 END
SET @Is0700 = CASE WHEN @hourOfDay = 7 THEN 1 ELSE 0 END
SET @Is0800 = CASE WHEN @hourOfDay = 8 THEN 1 ELSE 0 END
SET @Is0900 = CASE WHEN @hourOfDay = 9 THEN 1 ELSE 0 END
SET @Is1000 = CASE WHEN @hourOfDay = 10 THEN 1 ELSE 0 END
SET @Is1100 = CASE WHEN @hourOfDay = 11 THEN 1 ELSE 0 END
SET @Is1200 = CASE WHEN @hourOfDay = 12 THEN 1 ELSE 0 END
SET @Is1300 = CASE WHEN @hourOfDay = 13 THEN 1 ELSE 0 END
SET @Is1400 = CASE WHEN @hourOfDay = 14 THEN 1 ELSE 0 END
SET @Is1500 = CASE WHEN @hourOfDay = 15 THEN 1 ELSE 0 END
SET @Is1600 = CASE WHEN @hourOfDay = 16 THEN 1 ELSE 0 END
SET @Is1700 = CASE WHEN @hourOfDay = 17 THEN 1 ELSE 0 END
SET @Is1800 = CASE WHEN @hourOfDay = 18 THEN 1 ELSE 0 END
SET @Is1900 = CASE WHEN @hourOfDay = 19 THEN 1 ELSE 0 END
SET @Is2000 = CASE WHEN @hourOfDay = 20 THEN 1 ELSE 0 END
SET @Is2100 = CASE WHEN @hourOfDay = 21 THEN 1 ELSE 0 END
SET @Is2200 = CASE WHEN @hourOfDay = 22 THEN 1 ELSE 0 END
SET @Is2300 = CASE WHEN @hourOfDay = 23 THEN 1 ELSE 0 END
INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
(SendMonday = @IsMonday AND
SendTuesday = @IsTuesday AND
SendWednesday = @IsWednesday AND
SendThursday= @IsThursday AND
SendFriday = @IsFriday AND
SendSaturday = @IsSaturday AND
SendSunday = @IsSunday AND
Send0000 = @Is0000 AND
Send0100 = @Is0100 AND
Send0200 = @Is0200 AND
Send0300 = @Is0300 AND
Send0400 = @Is0400 AND
Send0500 = @Is0500 AND
Send0600 = @Is0600 AND
Send0700 = @Is0700 AND
Send0800 = @Is0800 AND
Send0900 = @Is0900 AND
Send1000 = @Is1000 AND
Send1100 = @Is1100 AND
Send1200 = @Is1200 AND
Send1300 = @Is1300 AND
Send1400 = @Is1400 AND
Send1500 = @Is1500 AND
Send1600 = @Is1600 AND
Send1700 = @Is1700 AND
Send1800 = @Is1800 AND
Send1900 = @Is1900 AND
Send2000 = @Is2000 AND
Send2100 = @Is2100 AND
Send2200 = @Is2200 AND
Send2300 = @Is2300)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
SendMonday= @IsMonday AND
SendTuesday = @IsTuesday AND
SendWednesday = @IsWednesday AND
SendThursday= @IsThursday AND
SendFriday = @IsFriday AND
SendSaturday = @IsSaturday AND
SendSunday = @IsSunday AND
Send0000 = @Is0000 AND
Send0100 = @Is0100 AND
Send0200 = @Is0200 AND
Send0300 = @Is0300 AND
Send0400 = @Is0400 AND
Send0500 = @Is0500 AND
Send0600 = @Is0600 AND
Send0700 = @Is0700 AND
Send0800 = @Is0800 AND
Send0900 = @Is0900 AND
Send1000 = @Is1000 AND
Send1100 = @Is1100 AND
Send1200 = @Is1200 AND
Send1300 = @Is1300 AND
Send1400 = @Is1400 AND
Send1500 = @Is1500 AND
Send1600 = @Is1600 AND
Send1700 = @Is1700 AND
Send1800 = @Is1800 AND
Send1900 = @Is1900 AND
Send2000 = @Is2000 AND
Send2100 = @Is2100 AND
Send2200 = @Is2200 AND
Send2300 = @Is2300