How to read an XML data in sql server? [duplicate]

vltsax25  于 2023-04-28  发布在  SQL Server
关注(0)|答案(1)|浏览(144)

This question already has answers here:

Xquery get value from attribute (2 answers)
Closed 3 days ago.

I have an XML

<Table>
  <Columns>
  <Column Name='Name' Datatype='varchar(100)'/>
  </Columns>
  <Rows>
  <Row Name='Test' Number='123'/>
  </Rows>
</Table>

I want to read Name value in the provided xml in sql server How Can I do that?

I tried using

`Declare @XMl XML 
select @xml= cast(Data as xml) from Table
select y.value('Name[1]','varchar(100)') as Name
@xml.nodes(//Row) as x(y)`

but am getting null values as result set How can I acheieve this in sql server

2ul0zpep

2ul0zpep1#

Like this:

declare @doc xml = 
    '<Table>
      <Columns>
      <Column Name="Name" Datatype="varchar(100)"/>
      </Columns>
      <Rows>
      <Row Name="Test" Number="123"/>
      </Rows>
    </Table>'
    
    select r.value('@Name', 'varchar(200)') Name
    from @doc.nodes('/Table/Rows/Row') n(r)

相关问题