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 “,” 用逗号分隔

posted @ 2016-06-28 16:43  淡墨青云  阅读(3344)  评论(0编辑  收藏  举报