别名 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 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:别名 FOR XML PATH 结果

基础教程推荐