0

次の構造 (ScheduledEmails) を持つテーブルを使用する必要があり、1 (true) に設定されている複数の変数を処理する最も効率的な方法を探しています。

以下のスクリプトは、1 つだけが選択されている場合にのみ正しい値を返します。

複数の条件に対応するために、一番下のクエリを変更する方法を誰かが推奨できますか?

たとえば、@IsMonday と @IsTuesday が true の場合などは?

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
4

1 に答える 1

1

あなたの要件を正しく理解していれば、ScheduledEmails テーブルから現在の曜日と時間のレコードを選択し、ScheduledEmailQueue テーブルに挿入するだけです。その場合は、動的 SQL クエリを作成して実行できます。あなたの要件を間違って理解した場合は、事前にお詫び申し上げます:)

DECLARE @dayOfWeek VARCHAR(20)
DECLARE @hourOfDay INT
DECLARE @hourOfDayColumnName VARCHAR(20)
DECLARE @dayOfWeekColumnName VARCHAR(20)
DECLARE @sql VARCHAR(MAX)

SET @dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET @hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET @hourOfDayColumnName = 'Send' + CASE WHEN LEN(@hourOfDay) = 1 THEN '0' + CONVERT(VARCHAR, @hourOfDay) ELSE CONVERT(VARCHAR, @hourOfDay) END + '00'
SET @dayOfWeekColumnName = 'Send' + @dayOfWeek
SET @sql = 'INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE ' + @dayOfWeekColumnName + ' = 1 AND ' + @hourOfDayColumnName + ' = 1'

PRINT @hourOfDayColumnName
PRINT @dayOfWeekColumnName
PRINT @sql
EXEC (@sql)

申し訳ありませんが、私はあなたの質問を読み違えましたが、あなたの要件についてはまだ明確ではありません. DayOfWeek と Hour を使用すると、常に特異値が返されます (例: Monday と 22)。複数の基準に基づいて値を選択する場合は、次のような単純なハードコーディングされたクエリを実行する必要があります。

SELECT Id, GETDATE() FROM ScheduledEmails WHERE 
SendMonday= 1 AND
SendTuesday = 1 AND
Send0000 = 1 AND
Send0100 = 1

必要な日数と時間の値が事前にわからない場合は、それらをパラメータとしてストアド プロシージャに渡し、フィルタリングに使用できます。

于 2012-09-12T15:29:19.760 に答える