看到论坛一个哥们问sql如何导出xml文件,就费力上网查查,研究下
--------------------------------------------------导出文件-----------------------------------
--输出为txt文件(任意文件)
exec master..xp_cmdshell 'bcp " select DepartID,DepartName,ParentID from rush.dbo.tb " queryout "c:/a.txt" /U"sa" /P"1234" /c'
go
/U 后为连接的sql服务器的用户名
/P 后为连接的sql服务器的密码
tb为一个表名
c:/a.txt为你要输出的路径
-------------------------------------------------导出xml--------------------------------------------
先在c盘建一个模板 a.tmp
<?xml version="1.0" standalone="yes"?>
<datatable xmlns="http://www.tempuri.org/DataSet1.xsd">
<%begindetail%>
<%insert_data_here%>
<%enddetail%>
</datatable>
--导出语句
EXEC sp_makewebtask
@outputfile = 'C:/Test.xml',
@query = 'Select * From tb for XML Auto,ELEMENTS',
@templatefile ='C:/a.tmp'
----------------------------------------------------------------
a.tmp可以为任意类型的文件
<datatable xmlns="http://www.tempuri.org/DataSet1.xsd">
此标签可以为任意的名称
例:
<rr xmlns="http://www.tempuri.org/ddd.xsd">
</rr>
@outputfile输出文件
@templatefile 模板文件
---------------------------------------------------------------
如果执行时提示服务未打开,可以自行下面命令即可
开启advanced options,Web Assistant Procedures服务
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Web Assistant Procedures', 1;
GO
RECONFIGURE
GO