문제

We've just switched to synonyms for linked server stuff, and noticed that our FOR XML output is no longer correct. When returning XML results from a view, we could alias the view and that would be assigned as the element name. With synonyms, however, it seems to ignore the alias? We're still mostly on SQL 2005 - this bug doesn't seem to happen on our 2008 instance. Is this a known problem, and any ideas for work-arounds?

For example, this is what we used to be able to do:

select top 3 number from Numbers as elementname for xml auto

<elementname number="0"/><elementname number="1"/><elementname number="2"/>

And this is what happens with a synonym:

select top 3 number from Numbers_synonym as elementname for xml auto 

<dbo.Numbers number="0"/><dbo.Numbers number="1"/><dbo.Numbers number="2"/>

As you can see, SQL Server seems to use the name of the actual referenced object instead of the alias. This gets worse for cross server queries, because you get the four-part name instead of the nice alias. (eg: <rick_server.rick_database.dbo.Numbers number="0"/>...)

도움이 되었습니까?

해결책

Sometimes FOR XML AUTO does the "wrong thing" - you can control the XML element name if you use the fractionally more verbose:

SELECT TOP 3 number
FROM Numbers_synonym
FOR XML PATH('elementname'), TYPE
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top