你可以看看这里
https://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
本文使用批量复制程序 (BCP) 创建 CSV 文件。虽然可以使用 DTS 或 SSIS 创建 CSV 文件,但使用 BCP 通常更简单、更高效。
我使用 master..sysobjects 作为示例表来提取。
创建一个简单的 CSV 文件
将数据从数据库表复制到文件的最简单方法是使用基本 BCP 命令:
BCP master..sysobjects out c:\sysobjects.txt -c -t, -T –S
用于创建 CSV 文件的 BCP 命令的基本格式如下:
BCP 输出
这里使用的开关是:
-c Output in ASCII with the default field terminator (tab) and row terminator (crlf)
-t override the field terminator with ","
-T use a trusted connection. Note that U –P may be used for username/password
-S connect to this server to execute the command
请注意,与 DTS/SSIS 一样,BCP 是一个客户端实用程序,因此您需要提供连接信息。
对于 SQL 服务器之间的数据传输,使用 -n 或 -N 代替 -c 来表示本机数据格式(-N = Unicode)。这要快得多,并且避免了数据转换问题。 BCP命令的完整格式请参考之前的BOL链接。
由于 BCP 是一个命令行实用程序,它使用 xp_cmdshell 从 T-SQL 执行。在你的 c: 驱动器上创建一个名为 BCP 的目录并执行:
declare @sql varchar(8000)select @sql = 'bcp master..sysobjects out
c:\bcp\sysobjects.txt -c -t, -T -S'+ @@servernameexec master..xp_cmdshell @sql