Sql 如果更新查询影响多行,则跳过该查询
可能重复:Sql 如果更新查询影响多行,则跳过该查询,sql,tsql,sql-server-2005,sql-update,Sql,Tsql,Sql Server 2005,Sql Update,可能重复: 我在SQLServer2005中有一个更新查询 update custom_graphics_files set actual_file_Name = @CorrectName where actual_file_Name = @FileName 现在,如果有多个实际的\u文件名,我想跳过更新查询,我喜欢为此使用窗口函数: update t set t.actual_file_Name = @CorrectName FROM custom_graphics_files t
我在SQLServer2005中有一个更新查询
update custom_graphics_files
set actual_file_Name = @CorrectName
where actual_file_Name = @FileName
现在,如果有多个
实际的\u文件名
,我想跳过更新查询,我喜欢为此使用窗口函数:
update t
set t.actual_file_Name = @CorrectName
FROM custom_graphics_files t
INNER JOIN
(
SELECT actual_file_Name, COUNT(*) TheCount
FROM custom_graphics_files
GROUP BY actual_file_Name
) t2 ON t.actual_file_Name = t2.actual_file_Name AND TheCount = 1
where t.actual_file_Name = @FileName;
with toupdate as (
select cgf.*, COUNT(*) over (PARTITION by actual_file_name) as ActCnt
from custom_graphics_files
)
update toupdate
set actual_file_Name = @CorrectName
where actual_file_Name = @FileName and ActCnt = 1
在大型表上,这可能不是最有效的解决方案,这取决于actual\u file\u Name=@FileName
的选择性。这是您可以得到的最“可读”的查询:
update custom_graphics_files
set actual_file_Name = @CorrectName
where actual_file_Name = @FileName
and (select count(1) from custom_graphics_files where actual_file_Name = @FileName) = 1