Select data from XML file as table in TSQL

后端 未结 2 1672
一生所求
一生所求 2021-02-04 05:47

Could someone show me some TSQL to use to query an xml file as if it were a table?

The file is on the server, \"C:\\xmlfile.xml\"

And contains

&l         


        
2条回答
  •  醉话见心
    2021-02-04 06:20

    In my case - the data I was interested in was contained in the node attributes rather than values. Below includes an example of how the attributes can be accessed.

    DECLARE @xmlData XML
    set @xmlData='
    
    
    
    
    
    
    
    '
    
    
    SELECT 
      ref.value('@FilterID', 'int') AS FilterID ,
      ref.value('@Name', 'NVARCHAR (10)') AS Name ,
      ref.value('@Code', 'NVARCHAR (10)') AS Code ,
      ref.value('@Department', 'NVARCHAR (3)') AS Department,
      ref.value('@Number', 'int') AS Number      
    FROM @xmlData.nodes('/ArrayOfSpangemansFilter/SpangemansFilter') 
    xmlData( ref )
    

提交回复
热议问题