在此blog post about exporting data to XML使用 BCP 或 SQLCMD 的 TSQL 我遇到了这行代码:
:XML ON
SELECT
*
FROM
dbo02.ExcelTest
FOR XML AUTO, ELEMENTS, ROOT('doc')
我尝试在 sql 查询中使用它,然后在批处理文件中通过 sqlcmd 触发,它确实返回了正确的 xml 文件。如果没有 :XML ON,它会返回网页上描述的奇怪值。
奇怪的是,SSMS (2008) 在解析或执行查询时显示不正确的语法。
:XML ON 有什么作用以及如何使用它?为什么 SSMS 不识别代码行?
最佳答案
在这个 MSDN 页面(搜索“:XML”)中有解释:sqlcmd Utility
该页面指出:
XML output that is the result of a FOR XML clause is output, unformatted, in a continuous stream.
When you expect XML output, use the following command: :XML ON.
Note
sqlcmd returns error messages in the usual format. Notice that the error messages are also output in the XML text stream in XML format. By using :XML ON, sqlcmd does not display informational messages.要关闭 XML 模式,请使用以下命令::XML OFF。
围棋 命令不应出现在发出 XML OFF 命令之前,因为 XML OFF 命令将 sqlcmd 切换回面向行的输出。
XML (流式)数据和行集数据不能混合。如果 XML ON 命令 尚未在输出 XML 的 Transact-SQL 语句之前发出 streams 被执行,输出将是乱码。如果 XML ON 命令 已发出,您不能执行 Transact-SQL 语句 输出常规行集。
Note
The :XML command does not support the SET STATISTICS XML statement.
在 SSMS 中,即使启用“SQLCMD 模式”(在“查询”菜单中),尝试使用 :XML 命令时也会出现以下错误:
Scripting warning.
Command Xml is not supported. String was not processed.
这很可能是由于在查询编辑器的上下文中不需要此特定命令。
关于xml - 什么是 :XML ON in T-SQL query to export data into XML using SQLCMD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24342724/