Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XQUERY - How to use the sql:variable in 'value()' function?

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)
like image 447
Angshuman Agarwal Avatar asked Aug 11 '11 10:08

Angshuman Agarwal


People also ask

How do I select a value from XML in SQL?

A.The value() method retrieves the ProductID attribute value from the XML. The value is then assigned to an int variable. Value 1 is returned as a result.

What is value keyword in SQL?

The VALUE keyword provides a way to return JSON value. Let's take a look at a simple example. Following is the query with VALUE keyword. SELECT VALUE "Hello World, this is DocumentDB SQL Tutorial"

What is SQL XQuery?

XQuery is a language that can query structured or semi-structured XML data. With the xml data type support provided in the Database Engine, documents can be stored in a database and then queried by using XQuery.


2 Answers

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)
like image 72
Mikael Eriksson Avatar answered Oct 15 '22 12:10

Mikael Eriksson


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

;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org')
select N.value('.', 'varchar(100)') as Value
from @T as T
  cross apply T.XMLCol.nodes('//*[local-name()=sql:variable("@Node")]/children/*[local-name(.)=sql:variable("@ChildName")]') as X(N)
like image 44
Dalex Avatar answered Oct 15 '22 11:10

Dalex