SQLServer使用命令导出数据库中数据到指定文件
打开SQL Server Management Studio,连接到数据库。
右键选择方面,把xp_cmdshell选择True。
按新建查询输入如下,即可将数据保存Excel表。
EXEC master..xp_cmdshell 'bcp "SELECT * FROM Data.dbo.DTable" queryout "G:\11\output.csv" -c -t, -T -S localhost'
表需要使用Data.dbo.DTable格式,最后的-S,需要写localhost,写IP地址无法连接,未能找到原因。
需要插入指定列标题文字可以用下面命令,注意列标题个数需要和选择的表个数一样
EXEC master..xp_cmdshell 'bcp "SELECT ''列名1'', ''列名2'', ''列名3'' UNION ALL SELECT * FROM Data.dbo.Dtable" queryout "G:\11\output.csv" -c -t, -T -S localhost'