SQL Server 啟用 xp_cmdshell 與bcp 使用


啟用 xp_cmdshell

  1: sp_configure 'show advanced options',1
  2: reconfigure
  3: GO
  4: 
  5: sp_configure 'xp_cmdshell',1
  6: reconfigure
  7: go
  8: 

BCP測試

  1:    
  2:    --建立文件夾
  3:    DECLARE  @CmdLine VARCHAR(500)=' md E:\NetworkOptFtpServer'
  4:    PRINT @CmdLine
  5:    EXEC master.. xp_cmdshell @CmdLine
  6:    
  7:    --生成文件 
  8:    SET  @CmdLine  = 'bcp " SELECT  * FROM table  " queryout  E:\NetworkOptFtpServer\test_in.csv -w -U sa  -P xxxx '
  9:    PRINT @CmdLine
 10:    EXEC master.. xp_cmdshell @CmdLine

指定編碼格式

  1: exec master..xp_cmdshell
  2: 'bcp "SELECT * FROM tablename" queryout "C:\test.txt" -T -c -C 65001'


最后的參數 -T 表示這是信任的連接,也就是說不用輸入密碼了;-c 表示按字符類型輸出;

-t “,” 用逗號分隔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM