首页 > 解决方案 > 将多个更新语句合并为一个更新

问题描述

我有一个 sql server 2012 数据库,该数据库有一个表,其中有一列名为“gameRoomText”,其数据如下所示:

<p>
<img id="4e" style="margin: 5px" 
title="GameFiles/1854/4e.jpg (media placeholder image)" 
src="../images/nothing_null.png" 
alt="GameFiles/1854/4e.jpg (media placeholder image)" 
width="320" height="380" /></p>

我需要删除所有包含如下文本的行:

src="../images/nothing_null.png"

所以我首先去掉字符串 " (media placeholder image)" :

update gameList
set gameText = replace(gameText, ' (media placeholder image)', '')
where gameID = '1854'
and tileID = '0FE'
and gameText LIKE '%src="../images/nothing_null.png"%'

然后我删除现有的 src 标签:

update gameList
set gameText = replace(gameText, 'src="../images/nothing_null.png" ', '')
where gameID = '1854'
and tileID = '0FE'

现在我将“alt”标签更改为“src”标签:

update gameList
set gameText = replace(gameText, 'alt=', 'src=')
where gameID = '1854'
and tileID = '0FE'  

最后,我将“../”添加到新的“src”标签路径中,如下所示:

update gameList
set gameText = replace(gameText, 'src="', 'src="../')
where gameID = '1854'
and tileID = '0FE'  

这些工作,但是我想知道是否有办法将这 4 个更新语句组合成一个,这样我只需要为每个可能有这些问题的游戏图块“tileID”和游戏“gameID”运行一个更新语句。

谢谢!

标签: tsqlsql-server-2012

解决方案


您可以链接替换语句,但它会很快变得混乱。例如:

update gameList
set gameText = replace(replace(replace(replace(gameText, ' (media placeholder image)', ''), 'src="../images/nothing_null.png" ',''), 'alt=', 'src='), 'src="', 'src="../')
where gameID = '1854'
and tileID = '0FE'
and gameText LIKE '%src="../images/nothing_null.png"%'

推荐阅读