简体   繁体   English

MS SQL Server 2008 R2中的CASE错误

[英]CASE Error in MS SQL Server 2008 R2

I have a query that work in an INSERT statement but not as a stand alone, and for the life of me I can't figure out why. 我有一个查询可以在INSERT语句中运行,但不能独立运行,对于我来说,我不知道为什么。 Here's the code that works: 这是有效的代码:

declare @FY1 char(4)
set @FY1 = (DATEPART(yy,DATEADD(m,-2,GETDATE()) ))
declare @fy char(2)
set @fy = SUBSTRING(@fy1,3,2)
declare @MonthUnits char (2)
set @MonthUnits = datepart(mm, dateadd(mm, -2, getdate()))

create table #tmpJDEnbrunits (mcmcu char(12)
        , NbrUnits float
        , mcdl02 char(40))
insert into #tmpJDEnbrunits
select '000' + ltrim(MCMCU)
, CASE @MonthUnits 
WHEN  1 THEN GBAN01/100 -- January
WHEN  2 THEN GBAN02/100 -- February
WHEN  3 THEN GBAN03/100 -- March
WHEN  4 THEN GBAN04/100 -- April
WHEN  5 THEN GBAN05/100 -- May
WHEN  6 THEN GBAN06/100 -- June
WHEN  7 THEN GBAN07/100 -- July
WHEN  8 THEN GBAN08/100 -- August
WHEN  9 THEN GBAN09/100 -- September
WHEN 10 THEN GBAN10/100 -- October
WHEN 11 THEN GBAN11/100 -- November
WHEN 12 THEN GBAN12/100 -- December
  END
, mcdl02 
from [JDEPSQL1\JDEProd].JDE_PRODUCTION.PRODDTA.F0902, 
[JDEPSQL1\JDEProd].JDE_PRODUCTION.PRODDTA.F0006 
where GBMCU = MCMCU and 
GBFY = @fy and GBLT = 'AU' and
GBOBJ = '9900' and GBSUB = '006' and
MCSTYL in ('R') AND MCMCU<'         999' AND 
MCRP22<>'X'

select * from #tmpJDEnbrunits
where NbrUnits > 0

But when I try to run just the SELECT with the CASE to eliminate the temp table, like this: 但是,当我尝试仅使用CASE运行SELECT来消除临时表时,如下所示:

declare @FY1 char(4)
set @FY1 = (DATEPART(yy,DATEADD(m,-2,GETDATE()) ))
declare @fy char(2)
set @fy = SUBSTRING(@fy1,3,2)
declare @MonthUnits char (2)
set @MonthUnits = datepart(mm, dateadd(mm, -2, getdate()))

select '000' + ltrim(MCMCU)
, CASE @MonthUnits 
    WHEN  1 THEN GBAN01/100 -- January
    WHEN  2 THEN GBAN02/100 -- February
    WHEN  3 THEN GBAN03/100 -- March
    WHEN  4 THEN GBAN04/100 -- April
    WHEN  5 THEN GBAN05/100 -- May
    WHEN  6 THEN GBAN06/100 -- June
    WHEN  7 THEN GBAN07/100 -- July
    WHEN  8 THEN GBAN08/100 -- August
    WHEN  9 THEN GBAN09/100 -- September
    WHEN 10 THEN GBAN10/100 -- October
    WHEN 11 THEN GBAN11/100 -- November
    WHEN 12 THEN GBAN12/100 -- December
  END
, mcdl02 
from [JDEPSQL1\JDEProd].JDE_PRODUCTION.PRODDTA.F0902, 
[JDEPSQL1\JDEProd].JDE_PRODUCTION.PRODDTA.F0006 
where GBMCU = MCMCU and 
GBFY = @fy and GBLT = 'AU' and
GBOBJ = '9900' and GBSUB = '006' and
MCSTYL in ('R') AND MCMCU<'         999' AND 
MCRP22<>'X'

I get the following errors: 我收到以下错误:

Msg 8180, Level 16, State 1, Line 1 
Statement(s) could not be prepared.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Qry1043'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Qry1016'.
Msg 125, Level 15, State 4, Line 1
Case expressions may only be nested to level 10.

The last one is especially odd because the CASE statement has no nesting. 最后一个特别奇怪,因为CASE语句没有嵌套。 Any help will be appreciated, I've been Googling for an hour with no luck! 任何帮助将不胜感激,我已经搜寻了一个小时,没有运气!

EDIT: It literally seems to think the WHENs are nested CASE statements. 编辑:从字面上看,似乎认为WHEN是嵌套的CASE语句。 If I comment out any two of them it will run without errors. 如果我注释掉其中任何两个,它将正常运行。

You are using some strange auto conversion here or this is not your actual code. 您在这里使用了一些奇怪的自动转换,或者这不是您的实际代码。 If it is your code then do this so you compare strings to strings (not strings to numeric): 如果这是您的代码,请执行此操作,以便将字符串与字符串(而不是字符串与数字)进行比较:

CASE @MonthUnits 
    WHEN '01' THEN GBAN01/100 -- January
    WHEN '02' THEN GBAN02/100 -- February
    WHEN '03' THEN GBAN03/100 -- March
    WHEN '04' THEN GBAN04/100 -- April
    WHEN '05' THEN GBAN05/100 -- May
    WHEN '06' THEN GBAN06/100 -- June
    WHEN '07' THEN GBAN07/100 -- July
    WHEN '08' THEN GBAN08/100 -- August
    WHEN '09' THEN GBAN09/100 -- September
    WHEN '10' THEN GBAN10/100 -- October
    WHEN '11' THEN GBAN11/100 -- November
    WHEN '12' THEN GBAN12/100 -- December
  END

Note also, if you really want an number you can use the builtin function MONTH 另请注意,如果您确实想要一个数字,则可以使用内置函数MONTH

declare @MonthUnits int
set @MonthUnits = month(dateadd(mm, -2, getdate())

aside 在旁边

I would expect to see that exact error message if your code actually looked like this: 如果您的代码看起来像这样,我希望看到确切的错误消息:

CASE @MonthUnits 
    CASE WHEN '01' THEN GBAN01/100 -- January
    CASE WHEN '02' THEN GBAN02/100 -- February
    CASE WHEN '03' THEN GBAN03/100 -- March
    CASE WHEN '04' THEN GBAN04/100 -- April
    CASE WHEN '05' THEN GBAN05/100 -- May
    CASE WHEN '06' THEN GBAN06/100 -- June
    CASE WHEN '07' THEN GBAN07/100 -- July
    CASE WHEN '08' THEN GBAN08/100 -- August
    CASE WHEN '09' THEN GBAN09/100 -- September
    CASE WHEN '10' THEN GBAN10/100 -- October
    CASE WHEN '11' THEN GBAN11/100 -- November
    CASE WHEN '12' THEN GBAN12/100 -- December
  END

Here you have lots of case statements starting (nested) and no end statement for the case. 在这里,您有很多case语句的开始(嵌套),而没有end语句。

Try this code, 试试这个代码,

CASE 
    WHEN  @MonthUnits = '1' THEN GBAN01/100 -- January
    WHEN  @MonthUnits = '2' THEN GBAN02/100 -- February
    WHEN  @MonthUnits = '3' THEN GBAN03/100 -- March
    WHEN  @MonthUnits = '4' THEN GBAN04/100 -- April
    WHEN  @MonthUnits = '5' THEN GBAN05/100 -- May
    WHEN  @MonthUnits = '6' THEN GBAN06/100 -- June
    WHEN  @MonthUnits = '7' THEN GBAN07/100 -- July
    WHEN  @MonthUnits = '8' THEN GBAN08/100 -- August
    WHEN  @MonthUnits = '9' THEN GBAN09/100 -- September
    WHEN @MonthUnits = '10' THEN GBAN10/100 -- October
    WHEN @MonthUnits = '11' THEN GBAN11/100 -- November
    WHEN @MonthUnits = '12' THEN GBAN12/100 -- December
  END

You will not get "Case expressions may only be nested to level 10." 您不会得到“案例表达式只能嵌套到10级”。 error 错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM