且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 SQL Server 2008 生成 XML 文件

更新时间:2022-10-16 19:35:18

还有一个选择 - 使用 sqlcmd 工具.

  1. 添加 :XML ON 作为 SQL 文件的第一行(我们称之为 input.sql)
  2. 像这样的命令可以解决问题:

sqlcmd -S -i 输入.sql -o 输出.xml

I am working on an application where I need to get the SQL response as XML into an XML file (and to store it in some physical location, say c:\xyz.xml).

I am able to generate the XML content using the provisions available in SQL Server as shown below.

 SELECT * FROM @Table FOR XML AUTO, ELEMENTS

where: @Table is a table variable.

I want to know how I can store the query output to an XML file from SQL Server itself.

There's one more option - use sqlcmd tool.

  1. Add :XML ON as a first line in your SQL file (let's call it input.sql)
  2. A command like this will do the trick:

sqlcmd -S <your-server> -i input.sql -o output.xml