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

后端 未结 2 504
情歌与酒
情歌与酒 2020-12-19 09:10

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

2条回答
  •  [愿得一人]
    2020-12-19 09:55

    declare @T table(XMLCol xml)
    insert into @T values
    ('
      
        No this
        John
        
          
            GMT Standard Time
            GMT
          
        
      
    ') 
    
    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)
    

提交回复
热议问题