How do I update a XML string in an ntext column in SQL Server?(如何更新 SQL Server 中 ntext 列中的 XML 字符串?)
本文介绍了如何更新 SQL Server 中 ntext 列中的 XML 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个包含 2 列的 SQL 表.ID(int) 和值(ntext)
have a SQL table with 2 columns. ID(int) and Value(ntext)
值行中包含各种 xml 字符串.
The value rows have all sorts of xml strings in them.
ID Value
-- ------------------
1 <ROOT><Type current="TypeA"/></ROOT>
2 <XML><Name current="MyName"/><XML>
3 <TYPE><Colour current="Yellow"/><TYPE>
4 <TYPE><Colour current="Yellow" Size="Large"/><TYPE>
5 <TYPE><Colour current="Blue" Size="Large"/><TYPE>
6 <XML><Name current="Yellow"/><XML>
我该怎么做:
A.列出
`<TYPE><Colour current="Yellow",`
bearing in mind that there is an entry
<XML><Name current="Yellow"/><XML>
B.修改包含
<TYPE><Colour current="Yellow" to be
<TYPE><Colour current="Purple"
谢谢!4 你的帮助
推荐答案
在SQL Server 2005+
中,使用中间临时表:
In SQL Server 2005+
, using a intermediary temporary table:
DECLARE @q AS TABLE (xid INT NOT NULL, xdoc XML NOT NULL, modified TINYINT NOT NULL DEFAULT 0)
INSERT
INTO @q (xid, xdoc)
SELECT id, doc
FROM mytable
UPDATE @q
SET xdoc.modify('replace value of (/TYPE/@Colour)[1] with "blue"'),
modified = 1
WHERE xdoc.value('(/TYPE/@Colour)[1]', 'NVARCHAR(MAX)') = 'Yellow'
UPDATE mytable
SET doc = CAST(xdoc AS NVARCHAR(MAX))
FROM @q q
WHERE id = q.xid
AND q.modified = 1
这篇关于如何更新 SQL Server 中 ntext 列中的 XML 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何更新 SQL Server 中 ntext 列中的 XML 字符串?


基础教程推荐
猜你喜欢
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01