gpt4 book ai didi

sql - XQUERY - 如何使用 sql :variable in 'value()' function?

转载 作者:行者123 更新时间:2023-12-02 01:52:07 25 4
gpt4 key购买 nike

下面的查询尝试选择给定节点的子节点。如何使用变量而不是对子节点进行硬编码,以便可以将它们作为 SProc 中的参数传递?

declare @T table(XMLCol xml)
insert into @T values
('<Root xmlns="http://tempuri.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Elem1 type="T1">
<Name type="string" display="First name">John</Name>
<TimeZone display="Time zone">
<children>
<DisplayName type="string" display="Display name">GMT Standard Time</DisplayName>
<HiddenName type="string" display="Hidden name">GMT</HiddenName>
</children>
</TimeZone>
</Elem1>
</Root>')

declare @Node varchar(50)
set @Node = 'TimeZone'

select N.value('(children/DisplayName)[1]', 'varchar(100)') as Value
from @T as T
cross apply T.XMLCol.nodes('//*[local-name()=sql:variable("@Node")]') as X(N)

最佳答案

declare @T table(XMLCol xml)
insert into @T values
('<Root xmlns="http://tempuri.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Elem1 type="T1">
<DisplayName type="string" display="Display name">No this</DisplayName>
<Name type="string" display="First name">John</Name>
<TimeZone display="Time zone">
<children>
<DisplayName type="string" display="Display name">GMT Standard Time</DisplayName>
<HiddenName type="string" display="Hidden name">GMT</HiddenName>
</children>
</TimeZone>
</Elem1>
</Root>')

declare @Node1 varchar(50)
set @Node1 = 'TimeZone'

declare @Node2 varchar(50)
set @Node2 = 'DisplayName'

select N2.Value.value('.', 'varchar(100)') as Value
from @T as T
cross apply (select T.XMLCol.query('//*[local-name()=sql:variable("@Node1")]')) as N1(Value)
cross apply (select N1.Value.query('//*[local-name()=sql:variable("@Node2")]')) as N2(Value)

关于sql - XQUERY - 如何使用 sql :variable in 'value()' function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7024519/

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