以下のスクリプトですべてが正常に機能していましたが、SELECT 句を使用して Case ステートメントを追加しようとしたところ、テーブルに正しい日付を自動入力するためにこれを実行しました。
DECLARE @PitchType_Skey INT
DECLARE @Site_Skey INT
DECLARE @Capacity INT
DECLARE @StartDate DATE
DECLARE @EndDate DATE
SET @PitchType_Skey = 1
SET @Site_Skey = 1
SET @Capacity = 0
WHILE (@Site_Skey < 127)
BEGIN
IF @PitchType_Skey = 8
BEGIN
SET @PitchType_Skey = 1
SET @Site_Skey = @Site_Skey + 1
END
IF (@Site_Skey < 127)
BEGIN
Set @Capacity = (SELECT SiteWeighting From Site Where Site_Skey = @Site_Skey)
Set @Capacity = @Capacity * (SELECT PitchTypeWeighting From PitchType Where PitchType_Skey = @PitchType_Skey)
Set @Capacity = @Capacity * ((10*(100+((RAND()*40)-20)))*0.01)
INSERT INTO Capacity
SELECT
CASE @StartDate
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 1 THEN 01/05/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 2 THEN 01/04/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 3 THEN 01/04/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 4 THEN 01/01/2010) ELSE,
CASE @EndDate
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 1 THEN 30/09/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 2 THEN 01/11/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 3 THEN 01/11/2010) ELSE,
WHEN (SELECT SiteWeighting FROM Site WHERE SiteWeighting = 4 THEN 01/12/2010) ELSE,
round(@Capacity,0) as Capacity,
@PitchType_Skey, @Site_Skey
END
SET @PitchType_Skey = @PitchType_Skey + 1
END