在CASE语句中,NOT IN子句不在CASE语句中

时间:2017-03-16 13:05:17

标签: sql sql-server sql-update case case-when

我在存储过程中使用了以下查询:

[_|_]

问题: IF (@rowindex = 0 AND @sectionID IN (95, 165, 231, 232) AND @Priority = 0) BEGIN UPDATE SpecialTagContent SET IsVisible = CASE WHEN @sectionID = 95 AND TagName NOT IN ('Pet Videos') THEN @IsVisible WHEN @sectionID = 231 AND TagName NOT IN ('Ejercicios') THEN @IsVisible ELSE @IsVisible END WHERE TagName NOT IN ('GMO','Gardening','Mercury Free') AND SectionID = @sectionID END 针对NOT IN@sectionID = 95无效。但是,针对@sectionID = 231子句的NOT IN正在起作用。

结论 - 如果我通过WHERE@IsVisible=1,那么sectionID = 95列的所有行都应设置为IsVisible,但1之类的标记名除外1}}和Pet Videos的标记名。

注意 - WHERE CLAUSE的标记对WHERE clause95都是通用的。

请帮助解决如何实现这一目标!

我错过了什么......

2 个答案:

答案 0 :(得分:0)

更正TagName中的额外逗号和包裹isnull()以处理空值(coalesce()也可以):

TagName为空时,not in ()将返回nullNOT IN clause and NULL values

IF (@rowindex = 0 AND @sectionID IN (95, 165, 231, 232) AND @Priority = 0)
BEGIN          
    UPDATE SpecialTagContent 
      SET IsVisible = CASE
        WHEN @sectionID = 95
          AND coalesce(TagName,'') not in ('Pet Videos')
          THEN @IsVisible   
        WHEN @sectionID = 231
          AND coalesce(TagName,'') not in ('Ejercicios')
          THEN @IsVisible   
        ELSE @IsVisible
        END       
    WHERE coalesce(TagName,'') NOT IN ('GMO','Gardening','Mercury Free') 
      AND SectionID = @sectionID
 END

答案 1 :(得分:0)

它可能适合你

IF (@rowindex = 0 AND @sectionID IN ('95', '165', '231', '232') AND @Priority = 0)          

BEGIN          
UPDATE SpecialTagContent 
SET IsVisible = CASE
                   WHEN (@sectionID = 95 AND TagName NOT IN ('Pet Videos') )THEN @IsVisible   
                   WHEN (@sectionID = 231 AND TagName NOT IN ('Ejercicios') )THEN @IsVisible   
                   ELSE @IsVisible
                END       
WHERE TagName NOT IN ('GMO','Gardening','Mercury Free') 
  AND SectionID = @sectionID          
END