Getting multiple records from xml column with value() in SQL Server

前端 未结 3 1858
自闭症患者
自闭症患者 2020-12-10 06:47

This SQL only returns the first Activity element. How do I select them all? If I remove the [1] in the query I get an error that \"value() requires a singleton\".

3条回答
  •  萌比男神i
    2020-12-10 06:52

    This works, but seems unnecessarily complex. There may be an easier way.

     DECLARE @myDoc xml
        SET @myDoc = 
        '
            
                This is activity one
                This is activity two
                This is activity three
            
        '
    
    SELECT activity.VALUE('(//Activity)[1]','varchar(100)') AS activity
    FROM (
            SELECT NewTable.activity.query('.') AS activity
            FROM (SELECT 1 AS col1) AS t
            CROSS APPLY @myDoc.nodes('(/Root/Activities/Activity)') AS NewTable(activity)
         ) AS x
    

提交回复
热议问题