The query below is trying to select a child node of a given Node. How do I use a variable instead of hard coding the child node such that I can pass them as parameters in a 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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…