在SQL XML数据中更新节点结构

时间:2018-03-26 13:15:32

标签: sql sql-server xml

我在SQL Server中将一些数据存储为XML,如下所示:

<FormSearchFilter>
    .......
    <IDs>
        <int>1</int>
        <int>2</int>
    </IDs>
    .......
</FormSearchFilter>

此XML映射到DTO,ID的数据类型正在从List更改为字符串。因此,我现在需要更新所有现有XML:数据,如下所示:

<FormSearchFilter>
    .......
    <IDs>1,2</IDs>
    .......
</FormSearchFilter>

通过更新查询实现此目的的最佳方式

3 个答案:

答案 0 :(得分:1)

除了提示,这是一个非常糟糕的主意!你可能会尝试这样的事情:

DECLARE @t TABLE(
 Id INT NOT NULL IDENTITY(1,1),
 xml XML)

INSERT INTO @t(xml)
VALUES
 ('<FormSearchFilter><IDs><int>1</int><int>2</int></IDs></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int></IDs></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int><int>4</int></IDs></FormSearchFilter>');

 UPDATE @t
 SET [xml]= (SELECT REPLACE([xml].query('data(/FormSearchFilter/IDs/int)').value('.','nvarchar(max)'),' ',',') AS IDs 
             FOR XML PATH('FormSearchFilter'));

 SELECT * FROM @t

说明:

XQuery函数data()将返回由空格分隔的alle text()个节点(在您的情况下为int值)。可以用逗号替换它以获取所需的列表。

更新:保留其他元素(请注意,订单更改)

INSERT INTO @t(xml)
VALUES
 ('<FormSearchFilter><test>x</test><IDs><int>1</int><int>2</int></IDs></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int></IDs><test>x</test></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int><int>4</int></IDs></FormSearchFilter>');

 UPDATE @t
 SET [xml]= (SELECT  [xml].query('/FormSearchFilter/*[local-name()!="IDs"]') AS [*]
                    ,REPLACE([xml].query('data(/FormSearchFilter/IDs/int)').value('.','nvarchar(max)'),' ',',') AS IDs 
             FOR XML PATH('FormSearchFilter'));

 SELECT * FROM @t

答案 1 :(得分:0)

有点骇客,如果你对帮助表值函数持开放态度。

示例

Declare @XML xml = '
<FormSearchFilter>
    <OtherContent>Some Content</OtherContent>
    <IDs>
        <int>1</int>
        <int>2</int>
    </IDs>
    <IDs>
        <int>11</int>
        <int>12</int>
        <int>13</int>
    </IDs>
    <IDs>
        <int>99</int>
    </IDs>
    <MoreContent>Some MORE Content</MoreContent>
</FormSearchFilter>
'


Select @XML = replace(cast(@XML as varchar(max)),RetVal,NewVal)
 From (
        Select *
              ,NewVal = stuff(replace(replace(RetVal,'<int>',','),'</int>',''),1,1,'')
         From [dbo].[tvf-Str-Extract](cast(@XML as varchar(max)),'<IDs>','</IDs>')
      ) A

Select @XML

<强>返回

<FormSearchFilter>
  <OtherContent>Some Content</OtherContent>
  <IDs>1,2</IDs>
  <IDs>11,12,13</IDs>
  <IDs>99</IDs>
  <MoreContent>Some MORE Content</MoreContent>
</FormSearchFilter>

创建TVF是因为我厌倦了提取内容(左,右,charindex,patindex,反向......)。它是一个修改后的解析/拆分函数,它接受两个非类似的分隔符。只是为了说明,如果你要跑:

Select * From [dbo].[tvf-Str-Extract](cast(@XML as varchar(max)),'<IDs>','</IDs>')

结果将是

RetSeq  RetPos  RetVal
1       65      <int>1</int><int>2</int>
2       100     <int>11</int><int>12</int><int>13</int>
3       150     <int>99</int>

有兴趣的TVF

CREATE FUNCTION [dbo].[tvf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table 
As
Return (  

with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
       cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1) 
 From  (
        Select *,RetVal = Substring(@String, N, L) 
         From  cte4
       ) A
 Where charindex(@Delimiter2,RetVal)>1 

)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[tvf-Str-Extract] (@String,'[[',']]')
*/

答案 2 :(得分:0)

不是特别优雅,但最终会得到所需的输出:

DECLARE @t TABLE(
 Id INT NOT NULL IDENTITY(1,1),
 xml XML)

INSERT INTO @t(xml)
VALUES
 ('<FormSearchFilter><IDs><int>1</int><int>2</int></IDs></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int></IDs></FormSearchFilter>'),
 ('<FormSearchFilter><IDs><int>1</int><int>2</int><int>3</int><int>4</int></IDs></FormSearchFilter>');

DECLARE @updates TABLE(
 Id INT,
 UpdatedValue XML
)

INSERT INTO @updates
SELECT 
 Id,
 (SELECT STUFF((
  SELECT 
   ',' + c.value('.', 'varchar')
  FROM @t t1
   CROSS APPLY t1.xml.nodes('//IDs/int') x(c)
  WHERE t1.Id = t.Id
  FOR XML PATH('')
 ), 1, 1, '') IDs
 FOR XML PATH(''))
FROM @t t

-- remove existing IDs node
UPDATE @t
 SET xml.modify('delete //IDs')

-- insert updated IDs node back in
UPDATE t
 SET xml.modify('insert sql:column("u.UpdatedValue") into (/FormSearchFilter)[1]')
FROM @t t
 JOIN @updates u ON t.Id = u.Id