SQL Server 2005 中存在空格问题的 FOR XML AUTO 和列名 [英] FOR XML AUTO and column name with space issue in SQL Server 2005

查看:19
本文介绍了SQL Server 2005 中存在空格问题的 FOR XML AUTO 和列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 SQL

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

当我发出此 SQL 时,未知数据将添加到带有我的字段名称的 xml 中.

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

我得到了像

<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>

它显示 而不是这个 ,因为我提到了带有空格的列别名,例如Emp ID".如何修改我的 SQL 作为结果空间不会被忽略我的意思是我的 xml 看起来像

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>

推荐答案

这不是有效的 XML,所以不要考虑它.

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

没问题

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

失败

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

多词元素名称无效

这篇关于SQL Server 2005 中存在空格问题的 FOR XML AUTO 和列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆