WHERE语法错误的SQL CASE

时间:2016-10-28 19:05:04

标签: sql sql-server case where

下午好 我在SQL编程,我有这个问题 守则是:

python -c "<your command here>'

但是当我编译代码时,会出现以下错误

    select 
    item.TX_Commercial_Family as 'Familia Comercial',
    sum(nota.NM_Invoice_Quantity) as 'Quantidade Faturada' ,
    sum(nota.NM_Material) as 'Valor Faturado' 
    from DW_DTS_Item item
    INNER JOIN DIS_DTS_Invoice_Fact nota ON item.SK_Item = nota.SK_Item
    inner join DW_DTS_Representative usuario on        nota.SK_Representative=usuario.SK_Representative
  inner join DW_DTS_Operation_Nature cfop on       cfop.SK_Operation_Nature=nota.SK_Operation_Nature
   where 
item.CD_Inventory_Group='30'
and (
    cfop.CD_CFOP='5101'
    OR cfop.CD_CFOP='6101'
    OR cfop.CD_CFOP='6107'
    OR cfop.CD_CFOP='6108'
    OR cfop.CD_CFOP='6109'
    OR cfop.CD_CFOP='6113'
    OR cfop.CD_CFOP='6401'
    )
and( 
    case
        when :number = '01' then nota.SK_Representative='05' 
    end

)
and nota.SK_Currency='1'
and nota.CD_Country='BRASIL'
and nota.DT_Sale_Forma_Bill_Exit between '2016-09-01' and '2016-09-30'
--and extract (year from nota.DT_Sale_Forma_Bill_Exit) = extract(year from CURRENT_DATE)

group by item.TX_Commercial_Family,usuario.TX_Representative,usuario.SK_Representative
order by usuario.TX_Representative asc   

关键是我必须传递一个参数并通过这个参数过滤器,SK_Representative将出现在代码中。

我已尝试过所有内容,但错误仍然存​​在

2 个答案:

答案 0 :(得分:1)

根据错误,您可能正在使用SQL Server。 将:number替换为@number

e.g。

declare @number int = 1;
select  @number + 1;
set     @number = @number * 10;
select  @number;

答案 1 :(得分:0)

如果您想使用数字作为参数,请使用&#39; @&#39;而不是&#39;:&#39; 因此,请使用以下代码段,其中您使用了case语句。

和(     案件         当@number =&#39; 01&#39;然后nota.SK_Representative =&#39; 05&#39;     端