CASE语句导致错误

时间:2016-02-29 23:25:34

标签: sql database sql-server-2008

declare @JANORG int;
declare @FEBORG int;
declare @MARORG int;
declare @APRORG int;
declare @MAYORG int; 
declare @JUNORG int;
declare @JULORG int;
declare @AUGORG int;
declare @SEPORG int;
declare @OCTORG int;
declare @NOVORG int;
declare @DECORG int;

select 
    p.orgid, p.projid, bomitem.fk_itemid, p.budgetyear,
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@JANORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@FEBORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@MARORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@APRORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@MAYORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@JUNORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@JULORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@AUGORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@SEPORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@OCTORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@NOVORG)/100 AS INT)),
    SUM(cast((bom.component_attachrate*bomitem.itemqty*@DECORG)/100 AS INT)) 
from 
    REF_VHF_COMPONENT bom, VHF_COMPONENT_ITEMS bomitem,
    VHF_PRJFCAST v, VHF_SYSGEN_PROJ p, VHF_ORGPT o 
where  
    bom.pk_ID = bomitem.fk_bomid
    and p.budgetyear = o.budgetyear 
    and CASE @JANORG WHEN v.JANORG = '0' THEN v.JANORG WHEN v.JANAREA = '0'                THEN     v.JANAREA ELSE p.JANORG END,
 CASE @FEBORG WHEN v.FEBORG <> '0' THEN v.FEBORG WHEN v.FEBAREA <> '0' THEN        v.FEBAREA ELSE p.FEBORG END,
          CASE @MARORG WHEN v.MARORG <> '0' THEN v.MARORG WHEN v.MARAREA <> '0' THEN v.MARAREA ELSE p.MARORG END,
 CASE @APRORG WHEN v.APRORG <> '0' THEN v.APRORG WHEN v.APRAREA <> '0' THEN v.APRAREA ELSE p.APRORG END,
 CASE @MAYORG WHEN v.MAYORG <> '0' THEN v.MAYORG WHEN v.MAYAREA <> '0' THEN v.MAYAREA ELSE p.MAYORG END,
 CASE @JUNORG WHEN v.JUNORG <> '0' THEN v.JUNORG WHEN v.JUNAREA <> '0' THEN v.JUNAREA ELSE p.JUNORG END,
 CASE @JULORG WHEN v.JULORG <> '0' THEN v.JULORG WHEN v.JULAREA <> '0' THEN v.JULAREA ELSE p.JULORG END,
 CASE @AUGORG WHEN v.AUGORG <> '0' THEN v.AUGORG WHEN v.AUGAREA <> '0' THEN v.AUGAREA ELSE p.AUGORG END,
 CASE @SEPORG WHEN v.SEPORG <> '0' THEN v.SEPORG WHEN v.SEPAREA <> '0' THEN v.SEPAREA ELSE p.SEPORG END,
 CASE @OCTORG WHEN v.OCTORG <> '0' THEN v.OCTORG WHEN v.OCTAREA <> '0' THEN v.OCTAREA ELSE p.OCTORG END,
 CASE @NOVORG WHEN v.NOVORG <> '0' THEN v.NOVORG WHEN v.NOVAREA <> '0' THEN v.NOVAREA ELSE p.NOVORG END,
 CASE @DECORG WHEN v.DECORG <> '0' THEN v.DECORG WHEN v.DECAREA <> '0' THEN v.DECAREA ELSE p.DECORG END 
group by 
    bomitem.fk_itemid, p.orgid, p.projid, bomitem.fk_itemid, p.budgetyear

我收到的错误是case语句*

的语法不正确

1 个答案:

答案 0 :(得分:0)

您的failureRedirect表达式不遵循两种正确格式中的任何一种。您可以确定要与之比较的值一次:

CASE

或者您可以使用更灵活的格式,其中每个CASE col1 WHEN 'dog' THEN 1 WHEN 'cat' THEN 2 WHEN 'horse' THEN 3 END 都有可以引用不同字段的综合标准:

WHEN

目前还不清楚您尝试使用CASE WHEN col1 = 'dog' THEN 1 WHEN col2 = 'horse' THEN 1 WHEN col3 = 'cat' THEN 2 END 表达式做什么。如果要使用它们进行过滤,则需要针对其他内容评估CASE表达式:

CASE