在 SQL Server 中将 SELECT 语句的结果导出到文件的方法

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

最后的参数 -T 表示这是信任的连接,也就是说不用输入密码了;-c 表示按字符类型输出;-C 65001 表示输出为 UTF-8 格式,其中 65001 是 UTF-8 的代码页。


bcp usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]

你可能感兴趣的:(在 SQL Server 中将 SELECT 语句的结果导出到文件的方法)