首先:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
然後:
--導出表的情況
EXEC master..XP_cmdshell 'bcp 數據庫名.dbo.表名 out "c:test.xls" /c -/S"服務器名" /U"用戶名" -P"密碼"'
--導出查詢的情況
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout "c:test.xls" /c -/S"服務器名" /U"用戶名" -P"密碼"'
發生以下錯誤:
[Error][Microsoft][Native]Error = [Microsoft][SQL Native Client]無法打開 BCP 主數據文件
使用如下命令:
EXEC xp_cmdshell 'ECHO %USERDOMAIN%\%USERNAME%'
返回 :NT AUTHORITY\NETWORK SERVICE然後在配置管理器(configuration manager)裏面的SQL server2005服務裏打開,看到登陸內置賬號爲Network service,
改成local system問題解決。