0

我很难找出为什么我的 t-sql 字符串会出现错误消息,如您所见,我创建了这个 sql 以从其他表中提取信息以创建容量表,但是有些不对劲,我不能弄清楚它是什么。你能帮我吗。提前致谢。

错误信息:

Msg 206, Level 16, State 2, Line 32
Operand type clash: int is incompatible with date

SQL脚本:

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 Capacity2
    SELECT
      CASE
        WHEN YEAR(@StartDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2010'
        WHEN YEAR(@StartDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2011'
        WHEN YEAR(@StartDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2012'
        WHEN YEAR(@StartDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2013'
        WHEN YEAR(@StartDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2014'
        WHEN YEAR(@StartDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '01 May 2015'

        WHEN YEAR(@StartDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '1 Apr 2010'
        WHEN YEAR(@StartDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '25 Mar 2011'
        WHEN YEAR(@StartDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '10 Mar 2012'
        WHEN YEAR(@StartDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '01 Apr 2013'
        WHEN YEAR(@StartDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '01 Mar 2014'
        WHEN YEAR(@StartDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '15 Mar 2015'

        WHEN YEAR(@StartDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '1 Apr 2010'
        WHEN YEAR(@StartDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '25 Mar 2011'
        WHEN YEAR(@StartDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '10 Mar 2012'
        WHEN YEAR(@StartDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '01 Apr 2013'
        WHEN YEAR(@StartDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '01 Mar 2014'
        WHEN YEAR(@StartDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '15 Mar 2015'

        WHEN YEAR(@StartDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2010'
        WHEN YEAR(@StartDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2011'
        WHEN YEAR(@StartDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2012'
        WHEN YEAR(@StartDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2013'
        WHEN YEAR(@StartDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2014'
        WHEN YEAR(@StartDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '01 Jan 2015'

        ELSE NULL
      END as StartDate,

      CASE
        WHEN YEAR(@EndDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2010'
        WHEN YEAR(@EndDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2011'
        WHEN YEAR(@EndDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2012'
        WHEN YEAR(@EndDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2013'
        WHEN YEAR(@EndDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2014'
        WHEN YEAR(@EndDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 1 THEN '30 Sep 2015'

        WHEN YEAR(@EndDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '01 Nov 2010'
        WHEN YEAR(@EndDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '05 Nov 2011'
        WHEN YEAR(@EndDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '02 Nov 2012'
        WHEN YEAR(@EndDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '08 Nov 2013'
        WHEN YEAR(@EndDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '01 Nov 2014'
        WHEN YEAR(@EndDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 2 THEN '03 Nov 2015'

        WHEN YEAR(@EndDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '01 Nov 2010'
        WHEN YEAR(@EndDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '05 Nov 2011'
        WHEN YEAR(@EndDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '02 Nov 2012'
        WHEN YEAR(@EndDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '08 Nov 2013'
        WHEN YEAR(@EndDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '01 Nov 2014'
        WHEN YEAR(@EndDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 3 THEN '03 Nov 2015'

        WHEN YEAR(@EndDate) = 2010 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2010'
        WHEN YEAR(@EndDate) = 2011 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2011'
        WHEN YEAR(@EndDate) = 2012 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2012'
        WHEN YEAR(@EndDate) = 2013 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2013'
        WHEN YEAR(@EndDate) = 2014 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2014'
        WHEN YEAR(@EndDate) = 2015 AND (SELECT SiteWeighting FROM Site WHERE Site_Skey=@Site_Skey) = 4 THEN '31 Dec 2015'

        ELSE NULL
      END,

      ROUND(@Capacity,0) as Capacity,
      @PitchType_Skey, @Site_Skey

  END
  SET @PitchType_Skey = @PitchType_Skey + 1

END
4

1 回答 1

2

您正在插入Capacity2表中,但没有指定将哪些值放入哪些列中。我猜这是问题的原因,它试图将您的其他整数值之一放入您的开始日期列。

您还没有发布您的架构,所以我只能猜测您的列名,但我建议您将脚本的插入部分更改为:

INSERT INTO Capacity2 (StartDate, EndDate, Capacity, PitchTypeKey, Site_SKey)
SELECT 
   CASE 
     WHEN ...

这样一来,您想将哪个值插入到哪一列中就没有歧义了。

于 2013-02-13T11:50:41.580 回答