别名 FOR XML PATH 结果

Aliasing a FOR XML PATH result(别名 FOR XML PATH 结果)
本文介绍了别名 FOR XML PATH 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为以下输出添加别名:

I want to alias the output of the following:

-- Test table with some rubbish data
DECLARE @Test TABLE
(
    Names [varchar](20)
)
INSERT INTO @Test
SELECT 'Simon'

-- Query returns but with XML_<GUID> alias
SELECT 
    Names
FROM 
    @Test t 
FOR XML PATH ('Test')

因此,为了争论,我想给它一个别名,而不是 XML_GUID 的列标题.我似乎无法得到它.有谁知道怎么做?我尝试以下示例:http://social.msdn.microsoft.com/Forums/nl/sqlxml/thread/1605c722-6388-40ff-9ab5-a3817a1db81f 但我似乎无法让它返回.我总是遇到错误,说第 1 列没有名称.

So rather than a column header of XML_GUID I want to give it an alias of say 'Test' for sake of argument. I can't seem to get it. Anyone know how? I tried following an example from here: http://social.msdn.microsoft.com/Forums/nl/sqlxml/thread/1605c722-6388-40ff-9ab5-a3817a1db81f but I can't seem to get it to return. I always run into the error that says the is no name for column 1.

感谢任何帮助.

谢谢,

西蒙

推荐答案

使其成为子查询:

select (
SELECT 
    Names
FROM 
    @Test t 
FOR XML PATH ('Test'),TYPE) as Test

子查询产生值但从不提供列的名称.我还指定了 ,TYPE 因为否则它会强制将结果转换为 varchar(max),而您可能希望将其保留为 xml.

Subqueries produce values but never provide a name for a column. I also specified ,TYPE because otherwise it forces a conversion to varchar(max) on the result, whereas you presumably want to keep it as xml.

这篇关于别名 FOR XML PATH 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)