摘要:
下文讲述使用bcp工具将数据库中数据表定向导出和使用bcp将文本文件中的数据导入到数据库中的方法分享,如下所示:
实验环境:sqlserver 2008 R2
例:
使用BCP导入导出数据的举例说明
---bcp使用前需设置sqlserver相关属性
--1。开启cmdshell
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
/*
导出 数据库 "test" 中的数据表 "tableName" 至D盘 20190604.txt文件中
sa 为sqlserver账户 123456 sqlserver中sa对应的密码
*/
EXEC master..xp_cmdshell 'bcp test..tableName out D:\20190604.txt -c -S -U sa -P 123456'
/*
导出 sql脚本中查询出的数据 至D盘 20190604.txt文件中
sa 为sqlserver账户 123456 sqlserver中sa对应的密码
*/
EXEC master..xp_cmdshell 'bcp "select top 10 * from test.dbo.tableName" queryout d:\20190604.txt -c -S -U sa -P 123456'
/*
导出 sql脚本中查询出的数据 至D盘 20190604.txt文件中
指定分隔符 t
sa 为sqlserver账户 123456 sqlserver中sa对应的密码
*/
EXEC master..xp_cmdshell 'bcp "select top 10 * from test.dbo.tableName" queryout d:\20190604.txt -c -t,-S -U sa -P 123456'