SQL Server 2008R2导出XML文件

-- 允许配置高级选项  
EXEC sp_configure 'show advanced options', 1  
GO  
-- 重新配置  
RECONFIGURE  
GO  
-- 启用xp_cmdshell  
EXEC sp_configure 'xp_cmdshell', 1  
GO  
--重新配置  
RECONFIGURE  
GO  
  
--执行想要的xp_cmdshell语句  
Exec xp_cmdshell 'query user'  
GO  
  
--用完后,要记得将xp_cmdshell禁用(从安全角度安全考虑)  
-- 允许配置高级选项  
EXEC sp_configure 'show advanced options', 1  
GO  
-- 重新配置  
RECONFIGURE  
GO  
-- 禁用xp_cmdshell  
EXEC sp_configure 'xp_cmdshell', 1  
GO  
--重新配置  
RECONFIGURE  
GO  

首先执行以上代码的上半部分,启用xp_cmdshell

执行语句用bcp导出到文件

-- 1.用信任连接
EXEC  xp_cmdshell 'bcp "select 列1,列2,列3 from 数据库名.dbo.表名 for xml path(''xml行'') ,root(''Root'')" queryout E:/123.xml -c -t -T  '
-- 2.用用户密码连接
exec xp_cmdshell 'bcp "select  列1,列2,列3  from 数据库名.dbo.表名  for xml path(''Item'')" queryout "c:\1.xml" -c -U "sa" -P "password"'
如果出现:无法打开bcp主数据文件

可能没有文件读写权限,改一个导出目录可不可以;登录账户没有权限,在SQL Sever Configuration Manager里面修改数据库服务器的内置账户为local system.
posted @ 2017-11-01 15:33  幸运的程序员  阅读(366)  评论(0)    收藏  举报