简体   繁体   English

如何在SQL Server的ntext列中更新XML字符串?

[英]How do I update a XML string in an ntext column in SQL Server?

have a SQL table with 2 columns. 有一个带有2列的SQL表。 ID(int) and Value(ntext) ID(int)和Value(ntext)

The value rows have all sorts of xml strings in them. 值行中包含各种xml字符串。

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>

How do I: 我如何:

A. List the rows where A.列出其中的行

`<TYPE><Colour current="Yellow",`
    bearing in mind that there is an entry
    <XML><Name current="Yellow"/><XML>

B. Modify the rows that contain B.修改包含以下内容的行

<TYPE><Colour current="Yellow" to be
<TYPE><Colour current="Purple"

Thanks! 谢谢! 4 your help 4你的帮助

In SQL Server 2005+ , using a intermediary temporary table: SQL Server 2005+ ,使用中间临时表:

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

Since it's an NTEXT field, you cannot use any of the usual string functions, unfortunately. 由于这是一个NTEXT字段,因此,您不能使用任何常用的字符串函数。

What version of SQL Server are you using?? 您正在使用哪个版本的SQL Server?

If you're on SQL Server 2005 and up, you have two options: 如果您使用的是SQL Server 2005及更高版本,则有两个选择:

  • you can cast your NTEXT to NVARCHAR(MAX) and then you can use all the usual string functions like REPLACE , SUBSTRING and so on 您可以将NTEXT强制转换为NVARCHAR(MAX),然后可以使用所有常见的字符串函数,例如REPLACESUBSTRING
  • you can cast your NTEXT to XML and use the XML functions available for SQL Server 2005 您可以将NTEXT转换为XML并使用可用于SQL Server 2005的XML函数

The first option could look like this: 第一个选项可能如下所示:

UPDATE 
  YourTable
SET
  Value = CAST(REPLACE(CAST(Value as NVARCHAR(MAX)), 
                       'Colour="Yellow"', 'Colour="Blue"') AS NTEXT) 
WHERE
  .......

For the second option, see Quasnoi's answer - however, mind you: your XML is a bit odd..... 对于第二种选择,请参阅Quasnoi的答案-但是,请注意:您的XML有点奇怪。

<TYPE><Colour="Yellow" Size="Large"></TYPE>

is a bit unusual and in my opinion invalid - either the "Colour" is an attribute on the <TYPE> tag 有点不正常,我认为无效-“颜色”是<TYPE>标记上的属性

<TYPE Colour="Yellow" Size="Large"></TYPE>

or then <Colour> in itself is a XML tag but then the "Yellow" must be assigned to an attribute: <Colour>本身就是XML标记,但是必须将“ Yellow”分配给属性:

<TYPE><Colour current="Yellow" Size="Large"></TYPE>

You cannot assign a value directly to the XML tag as you do in your XML, IMHO. 您不能像在XML IMHO中那样直接将值分配给XML标记。

If you're on SQL Server 2000, things will get a lot harder.... 如果您使用的是SQL Server 2000,事情会变得更加艰难。

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM