gpt4 book ai didi

sql-server - 使用T-SQL根据xml字段中的联接值更新表中的xml特定节点

转载 作者:行者123 更新时间:2023-12-03 16:08:46 26 4
gpt4 key购买 nike

我必须花很多时间和精力来解决这个问题。我什至几乎得到了输出,但是问题是导致我的问题的一对多联接。
这是我的数据

--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>'
*/


最终的解决方案是看起来像这样的代码,但不保留变量xml标签,例如 <tcode>在一个帐户中,而 <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


确实工作了,但又会成为一对多问题的受害者

最佳答案

那这个呢:

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的 .query()允许将其放入系列中:

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


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

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

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

关于sql-server - 使用T-SQL根据xml字段中的联接值更新表中的xml特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50091376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com