tsql - 将多个更新语句合并为一个更新
问题描述
我有一个 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”运行一个更新语句。
谢谢!
解决方案
您可以链接替换语句,但它会很快变得混乱。例如:
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"%'
推荐阅读
- python - 如何在 Linux 上的 QGIS 中安装第三方 python 库(例如 Pandas)?
- javascript - 如何防止由于调用钩子useSelector()而导致的重新渲染?
- java - 如何使用 Java 中的 Redisson 客户端更新条目的生存时间?
- git - 从 subrepo 中的提交创建提交消息
- python - 使用 API 进行 Python 多处理
- spring-boot - 在表达式 thymeleaf 中插入引号
- angular - 有什么方法可以仅在移动屏幕上以角度应用点击功能
- flutter - 在ListView中滚动地图(MapBox)时如何避免滚动ListView
- php - 内联 CSS 在 Wordpress 的 header.php 中不起作用
- naming-conventions - 首字母缩略词的不同骆驼壳叫什么?