問題描述
我想為以下輸出添加別名:
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 結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!