Working with the SQL Server XML data type

前端 未结 1 1287

I\'ve got a table which has a XML field. The typical XML it contains is;


  
    39
    

        
相关标签:
1条回答
  • 2021-01-04 21:44

    What i'm trying to achieve is a way of saying, across all xml data in the whole table... give me all of the xmlElements where the title is X.

    Not sure if I totally understood your question here - or are you looking for this? You would grab all the /things/Fruit elements a "nodes" and cross join them against your "base data" in the myTable - the result would be one row per XML element in your XML data field:

    select 
       omID,
       T.Fruit.query('.')
    from 
       dbo.myTable
    cross apply
       omText.nodes('/things/Fruit') as T(Fruit)
    where 
       T.Fruit.value('(title)[1]', 'varchar(50)') = 'X'
    

    Or give me a count of all items that use an imageId of 55.

    select 
       count(*)
    from 
       dbo.myTable
    cross apply
       omText.nodes('/things/Fruit') as T(Fruit)
    where 
       T.Fruit.value('(imageId)[1]', 'int') = 55
    

    Is that what you're looking for?

    Marc

    0 讨论(0)
提交回复
热议问题