SQL Server FOR XML 封闭元素? [英] SQL Server FOR XML Enclosing Element?

查看:29
本文介绍了SQL Server FOR XML 封闭元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SQL Server 2008,我有一个使用 FOR XML 发出结果集的查询.现在它是一个不合规的片段.

Using SQL Server 2008, I have a query that emits a result set using FOR XML. Right now it is a non-compliant fragment.

如何将我的结果 XML 包装在封闭元素中,然后在顶部放置一个简单的 XML 声明,并带有单个架构/命名空间引用以使输出符合要求?

How can I wrap my result XML in an enclosing element and then put a simple XML declaration on top with a single schema/namespace reference to make the output compliant?

谢谢.

推荐答案

SQL Server 中的 XML 数据类型不可能有 XML 处理指令.

It is not possible to have the XML processing instruction in a XML datatype in SQL Server.

请参阅XML 数据类型的限制

此代码

declare @XML xml =  
  '<?xml version="1.0"?>
   <root>Value</root>'

select @XML

有输出

<root>Value</root>

您可以将 XML 构建为带有 XML 处理指令的字符串.

You can build the XML as a string with the XML processing instruction in place.

declare @XML xml = '<root>Value</root>'
declare @XMLStr nvarchar(max) = '<?xml version="1.0"?>'
  
set @XMLStr = @XMLStr + cast(@XML as nvarchar(max))

select @XMLStr

输出

--------------------------------------------------------------------------
<?xml version="1.0"?><root>Value</root>

这篇关于SQL Server FOR XML 封闭元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆