将多个更新语句合并为一个更新

时间:2019-02-13 17:50:34

标签: tsql sql-server-2012

我有一个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”

因此,我首先摆脱字符串“((媒体占位符图像)”):

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”运行一个更新语句

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以链接replace语句,但是很快就会变得凌乱。例如:

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"%'