首页 > 解决方案 > 在 where in update 中的 case 表达式

问题描述

我有两个表 CopyMoveSources 和 ListItems。CopyMoveSources 有一个名为 TargetExists 的列。

我需要将 TargetExists 设置为 1 或 0,具体取决于文件是否存在于 ListItems 中。而且我必须只检查 TargetExists 为空的这些行,因此我不会在每次运行查询时检查相同的文件。我希望它是某种案例表达式,所有输入都受到赞赏。

select *
from [dbo].[CopyMoveSources] 
where source not in (select path from ListItems)
order by JobId asc

标签: sqlsql-server

解决方案


听起来你想要一个update

update CopyMoveSources
    set targetexists = (case when exists (select 1 from listitems li where li.path = copymovesources.source)
                             then 1 else 0
                        end)
    where targetexists is null

推荐阅读