简体   繁体   中英

optimizing sql xml select query

XML

<Data>
  <Language Name="ru-Ru">
    <Item Id="1">value</Item>
    <Item Id="2">value2</Item>
  </Language>
</Data>

Is it possible to run it somehow like this, to simply query:

SELECT id, 
       userId, 
       data.query('//Language[@Name="ru-Ru"]') AS myxml
  FROM UserData
 WHERE (myxml.query('//Item[@Id="9"]') like '%v%')

all in all I need parameter comparison to each item and do not want to repeat

'//Language[@Name="ru-Ru"]'

in each condition

You can not reuse aliased columns in where clause. You can use cross apply to do what you want. And you need to cast your where clause to be able to use like .

select
  id, 
  userId, 
  myxml
from @T
  cross apply
    (select data.query('//Language[@Name="ru-Ru"]')) as lang(myxml)
where cast(myxml.query('//Item[@Id="9"]') as varchar(max)) like '%v%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM