首先要啓動xp_cmdshell,最後還要關閉xp_cmdshell,中間執行導入導出語句。
默認狀況下,sql server2005安裝完後,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語句
Exec xp_cmdshell 'query user'
GO
----------------------------------------------------------------
--修改成
--------數據庫名----------------------數據庫名-----表名---------導入導出標誌-----導入導出位置---------服務名稱------用戶名---密碼
EXEC busDB..xp_cmdshell 'bcp busDB..FhCollectInfoTBbak in J:\FhCollectInfoTB.txt -c -Servername -Uname -Ppassword'
EXEC busDB..xp_cmdshell 'bcp busDB..FhCollectInfoTBbak out J:\FhCollectInfoTB.txt -c -Servername -Uname -Ppassword'
-----------
----------------------------------------------------------------
--用完後,要記得將xp_cmdshell禁用(出於安全考慮)
-- 容許配置高級選項
EXEC sp_configure 'show advanced options', 1
GO
-- 從新配置
RECONFIGURE
GO
-- 禁用xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 0
GO
--從新配置
RECONFIGURE
GOsql