T-SQL和案例陈述

时间:2013-02-13 11:44:47

标签: sql sql-server tsql case

我很难找到为什么我的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

1 个答案:

答案 0 :(得分:2)

您正在插入Capacity2表,而没有指定将哪些值放入哪些列以放入每个值。我猜这是导致问题的原因,并且它试图将您的其他整数值放入开始日期列。

您尚未发布架构,因此我只能猜测您的列名称,但我建议您将脚本的插入部分更改为:

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

这样就没有关于要将哪个值插入哪个列的歧义。