SQL WHERE 中的 CASE 语法不正确
SQL CASE in WHERE Incorrect Syntax
下午好
我在 SQL 中编程,我遇到了这个问题
代码是:
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
但是我编译代码的时候,出现如下错误
SQL Error [102][S0001]:Incorrect syntax near '='.
关键是我必须传递一个参数,并通过这个参数过滤器,通过它 SK_Representative 将出现在代码中。
我已经尝试了所有方法,但错误仍然出现
根据错误,您可能正在使用 SQL 服务器。
将 :number
替换为 @number
.
例如
declare @number int = 1;
select @number + 1;
set @number = @number * 10;
select @number;
如果您想使用数字作为参数,请使用“@”而不是“:”
因此,请在您使用案例语句的地方使用以下代码段。
和(
案件
当@number = '01' 然后 nota.SK_Representative='05'
结束
)
下午好 我在 SQL 中编程,我遇到了这个问题 代码是:
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
但是我编译代码的时候,出现如下错误
SQL Error [102][S0001]:Incorrect syntax near '='.
关键是我必须传递一个参数,并通过这个参数过滤器,通过它 SK_Representative 将出现在代码中。
我已经尝试了所有方法,但错误仍然出现
根据错误,您可能正在使用 SQL 服务器。
将 :number
替换为 @number
.
例如
declare @number int = 1;
select @number + 1;
set @number = @number * 10;
select @number;
如果您想使用数字作为参数,请使用“@”而不是“:” 因此,请在您使用案例语句的地方使用以下代码段。
和( 案件 当@number = '01' 然后 nota.SK_Representative='05' 结束
)