下面粘贴的是一个示例SQL代码,该代码在where子句中使用case语句,但是它抛出了一个语法错误,说“在mrktng_pckge_typ_cd和NOT关键字之间期待像END这样的关键字。”
CASE WHEN exc_ind=1 THEN
mrktng_pckge_typ_cd NOT IN ('a','b','c','d') ELSE NULL END
答案 0 :(得分:5)
where exc_ind <> 1
or mrktng_pckge_typ_cd NOT IN ('a','b','c','d')
如果exc_ind可以为NULL -
where coalesce (exc_ind,-1) <> 1
or mrktng_pckge_typ_cd NOT IN ('a','b','c','d')
出于演示目的:
where case when coalesce (exc_ind,-1) <> 1 or mrktng_pckge_typ_cd NOT IN ('a','b','c','d') then 1 else 0 end = 1
答案 1 :(得分:-1)
您可能需要这样做。我给出了不同的表名,但你可以这样做:
Declare @condition as int = 1
SELECT *
FROM mstCity
WHERE( (1=@condition and Name IN (SELECT AliasCity.Name From mstCity AliasCity WHERE AliasCity.Name NOT IN ('USA','UK') )) OR
(2=@condition AND Name IN (SELECT AliasCity.Name From mstCity AliasCity ))
)
根据您的查询更新的答案应如下所示:
WHERE ( ( exc_ind= 1 AND mrktng_pckge_typ_cd NOT IN ('a','b','c','d')) OR
( exc_ind <> 1 AND 1=1 )
)