问题描述
我想为以下输出添加别名:
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://www.51sjk.com/Upload/Articles/1/0/338/338295_20221206105613157.jpg 但我似乎无法让它返回.我总是遇到错误,说第 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://www.51sjk.com/Upload/Articles/1/0/338/338295_20221206105613157.jpg 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.
闲愁看客