简体   繁体   English

如何使用T-SQL内置函数中的值更新XML列?

[英]How to update an XML column with value from T-SQL built-in function?

I have a MS SQL 2008 R2 Standard database. 我有一个MS SQL 2008 R2标准数据库。 I have a column with varchar(250) data and a column with xml . 我有一个包含varchar(250)数据的列和一个包含xml的列。

CREATE TABLE [dbo].[art](
[id] [int] IDENTITY(1,1) NOT NULL,
[idstr] [varchar](250) NULL,
[rest] [xml] NOT NULL,
    CONSTRAINT [PK_art] PRIMARY KEY CLUSTERED ([id] ASC)
)

The problem is I want to insert result of a string function into into xml, in about 140 records. 问题是我想在大约140条记录中将字符串函数的结果插入到xml中。 I've tried to use xml.modify with dynamically generated text. 我试图使用动态生成的文本xml.modify

UPDATE [pwi_new].[dbo].[art]
SET rest.modify('insert <e><k>link</k><v>' 
      + my_string_function(idstr) + '</v></e> into (/root)[1]')
  WHERE parent = 160
  AND idstr LIKE '%&%'
GO

However, I've got this error: 但是,我有这个错误:

The argument 1 of the XML data type method "modify" must be a string literal.

Any ideas? 有任何想法吗? I'd like to avoid using temporal fields, external languages and executing TSQL from generated string? 我想避免使用时态字段,外部语言和从生成的字符串执行TSQL? (I've heard of sql:variable and sql:column , but this is a result of tsql function.) (我听说过sql:variablesql:column ,但这是tsql函数的结果。)

Not sure what you want to do here. 不知道你想在这做什么。 You mention a TSQL function and if that is the replace & to & it is not necessary. 你提到了一个TSQL函数,如果那是替换&to,则没有必要。 It is taken care of by SQL Server for you. 它由SQL Server为您处理。

A test using a table variable @art : 使用表变量@art测试:

declare @art table(parent int, idstr varchar(250), rest xml)

insert into @art values
(160, '123&456', '<root></root>')

update @art 
set rest.modify('insert (<e><k>link</k><v>{sql:column("idstr")}</v></e>) into (/root)[1]') 
where parent = 160 and
      idstr like '%&%'

select rest
from @art 

Result: 结果:

<root>
  <e>
    <k>link</k>
    <v>123&amp;456</v>
  </e>
</root>

Update 更新

For the not so trivial situations you can use a cross apply to get the values you need into a column. 对于不那么微不足道的情况,您可以使用交叉应用将所需的值添加到列中。

declare @art table(parent int, idstr varchar(250), rest xml)

insert into @art values
(160, '123&456', '<root></root>')

update a
set rest.modify('insert (<e><k>link</k><v>{sql:column("T.X")}</v></e>) into (/root)[1]') 
from @art as a
  cross apply (select reverse(replace(idstr, '2', '8')+'NotSoTrivial')) as T(X)
where parent = 160 and
      idstr like '%&%'

select rest
from @art 

Result: 结果:

<root>
  <e>
    <k>link</k>
    <v>laivirToStoN654&amp;381</v>
  </e>
</root>

Assuming your table has a key: 假设你的桌子有一把钥匙:

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + 'UPDATE [pwi_new].[dbo].[art] '
    + ' SET rest.modify(''insert <e><k>link</k><v>' 
    + REPLACE(idstr,'&','&amp;') 
    + '</v></e> into (/root)[1]'') WHERE key_column = ' 
    + CONVERT(VARCHAR(12), key_column) + ';'
FROM [pwi_new].[dbo].[art]
  WHERE parent = 160
  AND idstr LIKE '%&%';

PRINT @sql;
--EXEC sp_executesql @sql;

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

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