FOR XML AUTO and column name with space issue in SQL Server 2005

匆匆过客 提交于 2019-12-12 05:29:39

问题


Here is my SQL

select 
   ID as [Emp ID],
   Name as [Employee name],
   Sal as [Salary] 
from Emp 
FOR XML AUTO, ELEMENTS, ROOT('customers')

When I issue this SQL then unknown data is added in xml with my field name.

I got the xml like

<customers>
   <Emp>
     <Emp_x0020_ID>1</Emp_x0020_ID>
     <Employee_x0020_name>bob</Employee_x0020_name>
     <Salary>2020</Salary>
   </Emp>
   <Emp>
     <Emp_x0020_ID>2</Emp_x0020_ID>
     <Employee_x0020_name>keith</Employee_x0020_name>
     <Salary>6500</Salary>
   </Emp>
   <Emp>
     <Emp_x0020_ID>3</Emp_x0020_ID>
     <Employee_x0020_name>markc</Employee_x0020_name>
     <Salary>5400</Salary>
   </Emp>
 </customers>

It shows <Emp_x0020_ID> instead of this <Emp ID> because I mention column alias with space like "Emp ID". how could modify my SQL as a result space will not be ignore I mean my xml would look like

<customers>
  <Emp>
    <Emp ID>1</Emp ID>
    <Employee name>bob</Employee name>
    <Salary>2020</Salary>
   </Emp>
</customers>

回答1:


This is not valid XML, so don't even think about it.

This is OK

declare @xml xml
set @xml = '<a><bc>1</bc></a>'

This fails

declare @xml xml
set @xml = '<a><b c>1</b c></a>'

Multi-word element names are invalid



来源:https://stackoverflow.com/questions/4751297/for-xml-auto-and-column-name-with-space-issue-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!