开发者

Name XML result column of TSQL "for xml explicit"?

开发者 https://www.devze.com 2022-12-08 18:03 出处:网络
I have a query that uses for xml explit to return XML result. select ... from ... order by [BatchType!1!TypeName], [FormType!2!TypeName], Tag, Parent

I have a query that uses for xml explit to return XML result.

select ... from ... order by [BatchType!1!TypeName], [FormType!2!TypeName], Tag, Parent for xml explicit, root('ClientImages')

But the name of resultant column name is something as cryptic as

Name XML result column of TSQL "for xml explicit"?

Is there a way to change the column name?


[ANSWER] I had a several nested WITH statements so I have saved the result of query without applying FOR XML EXPLICIT into a temp table @xmlTable and then set the XML EXPLICIT result开发者_如何学C to an XML then returned it.

declare @xmlResult xml
set @xmlResult =(   
    select  * 
    from    @xmlTable
    for xml explicit, root('ClientImages'))

select  @xmlResult as XmlResult


-- add a select ( first) and type (on the end), and as [something]


select
    ( 
    select 1    as tag,
    null        as parent,
    'algo'      as [nodo!1!attr]
    for xml explicit, type
    ) as [MyDesiredName]




No, there is not, as far as I know. SQL Server Management Studio will autogenerate such a name for your XML.

You might be able to assign your output to an XML variable first and then select that to get a "nice" column name:

DECLARE @XmlResult XML

SELECT @XmlResult = ......
  FROM .......


SELECT @XmlResult

but other than that, I'm not aware of any way to influence the name of the column generated by SSMS.

Marc


If you need namespaces in your returned xml:

WITH XMLNAMESPACES ('urn:http://YourNamespaceHere' as NS0)
SELECT  ( ... ) AS XmlResult

You can use the NS0 namespace prefix in the sub select as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消