基于使用T-SQL在xml字段中连接值来更新表中的xml特定节点

时间:2018-04-29 21:12:45

标签: sql-server tsql xpath xquery xquery-sql

我必须花很多时间和搜索这个问题。我甚至几乎得到输出,但问题是导致我的问题的一对多连接。 这是我的数据

--notice some of the Account nodes have elements that others don't
DECLARE @tbl TABLE(ID INT,fkey int, YourXML XML)

INSERT INTO @tbl (id, fkey, YourXML)
    SELECT 
        98, 8,
        N'<Params>
    <Account>
        <FirstName>Michael</FirstName>
        <LastName>Bar</LastName>
        <tcode>8</tcode>
    </Account>
    <Account>
        <FirstName>Pam</FirstName>
        <LastName>Bar</LastName>
    </Account>
</Params>'

    UNION 

    SELECT 
        99, 9, 
        N'<Params>
    <Account>
        <FirstName>Phil</FirstName>
        <LastName>Foo</LastName>
    </Account>
    <Account>
        <FirstName>Rebecca</FirstName>
        <LastName>Foo</LastName>
        <whatever>argh</whatever>
    </Account>
</Params>'

DECLARE @tbl2 TABLE(id INT, fkey INT, sfirst VARCHAR(255), slast VRACHAR(255))

INSERT INTO @tbl2 (id, fkey, sfirst, slast)
    SELECT 1, 8, 'Michael', 'Bar'
    UNION
    SELECT 2, 8, 'Pam', 'Bar'
    UNION
    SELECT 3, 9, 'Phil', 'Foo'
    UNION
    SELECT 4, 9, 'Rebecca', 'Foo'

--expected output

/* expected output
<Params>
    <Account>
        <FirstName>first1</FirstName>
        <LastName>last1</LastName>
        <tcode>8</tcode>
    </Account>
    <Account>
        <FirstName>first2</FirstName>
        <LastName>last2</LastName>
    </Account>
     <Account>
        <FirstName>first3</FirstName>
        <LastName>last3</LastName>
    </Account>
    <Account>
        <FirstName>first4</FirstName>
        <LastName>last4</LastName>
        <whatever>argh</whatever>
    </Account>
</Params>'
*/

最终的解决方案是看起来像这样的代码,但没有保留一个帐户中的<tcode>等变量xml标记,以及另一个帐户中的<whatever>

;WITH cte AS
(
    SELECT 
        YourXML,
        (SELECT DISTICNT
             'first' + REPLACE(STR(se.ID,9),' ','') AS FirstName,
             'last' + REPLACE(STR(se.ID,9),' ','') AS LastName
         FROM 
             @tbl AS sea
         INNER JOIN
             @tbl2 se ON sea.fkey = se.fkey
         CROSS APPLY 
             sea.YourXML.nodes(N'/Params/Account') AS x(nth)
         WHERE 
             sea.id = ilv.id
         FOR XML PATH('Account'), ROOT('Params'), TYPE) AS NewAdditionalInfo
    FROM 
        @tbl AS ilv
)
UPDATE cte 
SET YourXML = NewAdditionalInfo;

然后我继续执行双嵌套while循环:1表示@tbl id,2表示count()表示有多少节点。

我注意到了

update @tbl
set YourXML.modify('replace value of (//Account/FirstName/text())[sql:variable("@tenantcount")][1] with concat(sql:column("sfirst"),sql:column("ValTbl.id") cast as xs:string ?)')
....
where @tbl.id = @current_id

确实有效,但又是一对多问题的受害者

2 个答案:

答案 0 :(得分:2)

感谢社区帮助

;WITH cte AS
(
    SELECT *
          ,(
                SELECT 
                t.YourXML
                        .query(N'/Params/Account[(FirstName/text())[1]=sql:column("sfirst") 
                                             and (LastName/text())[1]=sql:column("slast")]')
                        .query(N'<Account>
                                 {
                                    for $nd in /Account/*
                                    return 
                                    if(local-name($nd)="FirstName") then
                                        <FirstName>{concat("first"[1],xs:string(sql:column("t2.id")))}</FirstName>
                                    else if(local-name($nd)="LastName") then
                                        <LastName>{concat("last"[1],xs:string(sql:column("t2.id")))}</LastName>
                                    else
                                    $nd
                                 }  
                                 </Account> ') AS [*]
                FROM tbl AS t
                INNER JOIN tbl2 AS t2 ON t.fkey=t2.fkey
                where t.id = tt.id
                ORDER BY t2.id
                FOR XML PATH(''),ROOT('Params'),TYPE
           ) AS NewXML
    FROM tbl AS tt
)
update cte set YourXML = NewXML
select * from tbl

答案 1 :(得分:1)

这个怎么样:

SELECT t.YourXML
        .query(N'/Params/Account[(FirstName/text())[1]=sql:column("sfirst") 
                             and (LastName/text())[1]=sql:column("slast")]')
        .query(N'<Account>
                 {
                    for $nd in /Account/*
                    return 
                    if(local-name($nd)="FirstName") then
                        <FirstName>{concat($nd/text()[1],xs:string(sql:column("t2.id")))}</FirstName>
                    else if(local-name($nd)="LastName") then
                        <LastName>{concat($nd/text()[1],xs:string(sql:column("t2.id")))}</LastName>
                    else
                    $nd
                 }  
                 </Account> ') AS [*]
FROM @tbl AS t
INNER JOIN @tbl2 AS t2 ON t.fkey=t2.fkey
ORDER BY t2.id
FOR XML PATH(''),ROOT('Params');

结果

<Params>
  <Account>
    <FirstName>Michael1</FirstName>
    <LastName>Bar1</LastName>
    <tcode>8</tcode>
  </Account>
  <Account>
    <FirstName>Pam2</FirstName>
    <LastName>Bar2</LastName>
  </Account>
  <Account>
    <FirstName>Phil3</FirstName>
    <LastName>Foo3</LastName>
  </Account>
  <Account>
    <FirstName>Rebecca4</FirstName>
    <LastName>Foo4</LastName>
    <whatever>argh</whatever>
  </Account>
</Params>

一些解释

XML&#39; .query()允许将其放入系列:

SomeXML.query(N'XQuery').query(N'OtherXQuery')

第一个.query()使用sql:column过滤<Account>,其中FirstNameLastName等于sfirstslast
下一个.query()使用FLWOR查询遍历<Account>下面的所有节点,并根据其名称返回它们。
这样做,其他节点只是通过......

T-SQL XQuery相当有限,你无法动态定义新的元素或属性,也不能一般性地创建结构,但你可以做很多事情:-D

这不会更新原始表,但会准确返回您的预期输出。