在T-SQL中将XML数据转换为表格格式

在T-SQL中将XML数据转换为表格格式,sql,tsql,xquery-sql,Sql,Tsql,Xquery Sql,我想知道如何将XML分层数据转换为表格格式,并与SQLServer2005T-SQL(XPath/XQuery)中的其他关系数据库表连接 比如说, <Employees> <Employee ID="001" Name="David" /> <Employee ID="002" Name="Mike" /> <Employee ID="003" Name="Alex" /> <Employee ID="004" Name="M

我想知道如何将XML分层数据转换为表格格式,并与SQLServer2005T-SQL(XPath/XQuery)中的其他关系数据库表连接

比如说,

<Employees>
  <Employee ID="001" Name="David" />
  <Employee ID="002" Name="Mike" />
  <Employee ID="003" Name="Alex" />
  <Employee ID="004" Name="Morris" />
</Employees>
谢谢你的建议。:)

这里有一种方法:

declare @x xml

set @x = '<Employees>
              <Employee ID="001" Name="David" />
              <Employee ID="002" Name="Mike" />
              <Employee ID="003" Name="Alex" />
              <Employee ID="004" Name="Morris" />
          </Employees>'

select emp.e.value('@ID','varchar(10)') as ID, 
       emp.e.value('@Name','varchar(10)') as Name
    from @x.nodes('Employees/Employee') as emp(e)
declare@xxml
设置@x='1
'
选择emp.e.value(“@ID”,“varchar(10)”作为ID,
emp.e.value(“@Name”,“varchar(10)”作为名称
从@x.nodes(“员工/员工”)作为emp(e)

以下是Joe先前提交的答案的细微变化:

DECLARE @X xml
SET @X = '<Employees>
   <Employee ID="001" Name="David" />
   <Employee ID="002" Name="Mike" />
   <Employee ID="003" Name="Alex" />
   <Employee ID="004" Name="Morris" />
 </Employees>'

 SELECT
 [Employee].value('@ID','int')As ID,
 [Employee].value('@Name','varchar(10)') As Name
 FROM
 @x.nodes('/Employees/Employee') Employee([Employee])
DECLARE@xxml
设置@X='1
'
选择
[Employee].value('@ID','int')作为ID,
[Employee].value(“@Name”,“varchar(10)”作为名称
从…起
@x、 节点('/Employees/Employee')员工([Employee])
这是在MSSQL Server 2008 R2中完成的


我希望这个能帮助你

 declare @xml varchar(max)

 SET @xml = '<Employees>
  <Employee ID="001" Name="David" />
  <Employee ID="002" Name="Mike" />
  <Employee ID="003" Name="Alex" />
  <Employee ID="004" Name="Morris" />
</Employees>'

 Declare @documentHandler INT 

 EXEC sp_xml_preparedocument @documentHandler OUTPUT,@xml

 SELECT * 
 FROM OPENXML(@documentHandler,'/Employees/Employee')
       WITH (ID varchar(20),
             Name varchar(150))


 EXEC sp_xml_removedocument @documentHandler
declare@xml varchar(最大值)
SET@xml=
'
声明@documentHandler INT
EXEC sp_xml_preparedocument@documentHandler输出@xml
选择*
来自OPENXML(@documentHandler,“/Employees/Employees”)
ID为varchar(20),
姓名:varchar(150))
EXEC sp_xml_removedocument@documentHandler

这种较旧的方法效率较低,通常性能比其他两个答案中给出的技术差。是的。我见过几个案例,在这些案例中,我能够通过用新方法替换您的方法来提高性能。更糟糕的是,当你遇到一个开发人员忘记了
sp\u xml\u removedocument
@Joe:谢谢你,Joe,我在大多数宫殿里都用过这个sp,我想现在有更好的机会了+1请回答:)
 declare @xml varchar(max)

 SET @xml = '<Employees>
  <Employee ID="001" Name="David" />
  <Employee ID="002" Name="Mike" />
  <Employee ID="003" Name="Alex" />
  <Employee ID="004" Name="Morris" />
</Employees>'

 Declare @documentHandler INT 

 EXEC sp_xml_preparedocument @documentHandler OUTPUT,@xml

 SELECT * 
 FROM OPENXML(@documentHandler,'/Employees/Employee')
       WITH (ID varchar(20),
             Name varchar(150))


 EXEC sp_xml_removedocument @documentHandler