介绍 (Introduction)

The Bulk copy program aka bcp is the console application used to export and import data from text files to SQL Server or vice versa.

大容量复制程序(又称为bcp)是控制台应用程序,用于将数据从文本文件导出和导入到SQL Server,反之亦然。

It is very popular because it is fast and easy to download.

它非常流行,因为它快速且易于下载。

This tool is installed by default with SQL Server. It is usually installed in the following path:

默认情况下,此工具与SQL Server一起安装。 通常将其安装在以下路径中:

Drive: \\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\bcp.exe

驱动器:\\ Program Files \ Microsoft SQL Server \ Client SDK \ ODBC \ 130 \ Tools \ Binn \ bcp.exe

In this article we will learn how to:

在本文中,我们将学习如何:

  1. Get the bcp arguments 获取bcp参数
  2. Get the version 获取版本
  3. Export data from a SQL Server table to a file 将数据从SQL Server表导出到文件
  4. Export data from a SQL Server query to a file 将数据从SQL Server查询导出到文件
  5. Run bcp using PowerShell 使用PowerShell运行bcp
  6. Run bcp on SSIS 在SSIS上运行bcp
  7. Invoke a batch file in SSIS 在SSIS中调用批处理文件

要求 (Requirements)

  1. SQL Server installed 已安装SQL Server
  2. bcp installed bcp
  3. The Adventureworks database installed 已安装Adventureworks数据库
  4. SSIS Installed 已安装SSIS
  5. SSDT installed 已安装SSDT

开始吧 (Get started)

1. Get the bcp arguments

1.获取bcp参数

In the command line, write bcp. This will show you the possible arguments that can be used:

在命令行中,编写bcp。 这将向您显示可以使用的可能参数:

bcp /? Is the same than bcp. It shows you the possible parameters:

bcp /? 与bcp相同。 它显示了可能的参数:

2. Get the version

2.获取版本

You can get the version of the bcp using the -v argument:

您可以使用-v参数获取bcp的版本:

Here you have a list of versions of bcp:

这里有一个bcp版本列表:

3. Export data from a SQL Server table to a file

3.将数据从SQL Server表导出到文件

To export data from a SQL Server table or view to a file, you can use the bcp command.

要将数据从SQL Server表或视图导出到文件,可以使用bcp命令。

The following example exports the table person.person from the adventureworks database to a file named dimcustomer.bcp

下面的示例将AdventureWorks数据库中的表person.person导出到名为dimcustomer.bcp的文件中

bcp adventureworks.person.person out c:\sql\dimcustomer.bcp -c -T -S WIN-355CHQ0E524

bcp adventureworks.person.person输出c:\ sql \ dimcustomer.bcp -c -T -S WIN-355CHQ0E524

The -c argument is used to perform operations using a character type. -T is used to connect using a Trusted connection (Windows Authentication). -S is used to specify the SQL Server name.

-c参数用于使用字符类型执行操作。 -T用于使用可信连接(Windows身份验证)进行连接。 -S用于指定SQL Server名称。

If everything is OK, the rows will be copied successfully:

如果一切正常,将成功复制行:

You will receive the number of rows copied, the network packet size and the speed to copy the rows per second.

您将收到复制的行数,网络数据包大小以及每秒复制行的速度。

If everything is OK, the file will be created:

如果一切正常,将创建文件:

You can open the file and check the data:

您可以打开文件并检查数据:

4. Export data from a SQL Server query to a file

4.将数据从SQL Server查询导出到文件

You can export data from a SQL Server Query to a file. To do this, you need to specify the query in quotes and use the out argument:

您可以将数据从SQL Server查询导出到文件。 为此,您需要使用引号指定查询并使用out参数:

bcp adventureworks.person.person out c:\sql\dimcustomer.bcp -c -T -S WIN-355CHQ0E524

bcp adventureworks.person.person输出c:\ sql \ dimcustomer.bcp -c -T -S WIN-355CHQ0E524

The result displayed is the following:

显示的结果如下:

5. Run bcp using PowerShell

5.使用PowerShell运行bcp

PowerShell is a powerful tool to automate tasks using scripts and the command line. You can also run bcp using PowerShell, which is included with Windows and it can be also installed on Linux and Mac.

PowerShell是使用脚本和命令行自动执行任务的强大工具。 您还可以使用Windows随附的PowerShell运行bcp,也可以将其安装在Linux和Mac上。

In PowerShell create variables to store the database name, schema, table and output path:

在PowerShell中,创建变量以存储数据库名称,架构,表和输出路径:


$db = "adventureworks"
$schema = "person"
$table = "person"
$path = "C:\sql\powershell.txt"

Store the bcp command line with the parameters in another parameter:

将带有参数的bcp命令行存储在另一个参数中:


$psCommand = "bcp $($db).$($schema).$($table) out $path -T -c"

Use the Invoke-Expression to call the variable with the bcp commands:

使用Invoke-Expression通过bcp命令调用变量:

Invoke-Expression $psCommand

PowerShell will import the files in the powershell.txt file:

PowerShell将导入powershell.txt文件中的文件:

6. Run bcp on SSIS

6.在SSIS上运行bcp

It is not a common practice to run bcp on SSIS because SSIS contains tasks to import and export data. You can use the Data Flow task to create customized tasks or maybe use the Bulk Insert task to import data to SQL Server.

在SSIS上运行bcp是不常见的做法,因为SSIS包含导入和导出数据的任务。 您可以使用“数据流”任务来创建自定义任务,也可以使用“批量插入”任务将数据导入SQL Server。

However, there are some scenarios where you can invoke bcp in SSIS. For example, if you already have some command lines in bcp and you just want to invoke them. Another case is when you are adept at the command line and you already have some batch files ready and you just want to invoke them.

但是,在某些情况下,您可以在SSIS中调用bcp。 例如,如果您在bcp中已经有一些命令行,而您只想调用它们。 另一种情况是,您熟练使用命令行并且已经准备好了一些批处理文件,而您只想调用它们。

Open the SSDT and create a New Project.

打开SSDT并创建一个新项目。

In projects select Integration Services Project:

在项目中,选择Integration Services项目:

Drag and drop the Execute Process Task to the design pane:

执行过程任务拖放到设计窗格中:

In executable, specify the path of the bcp file:

在可执行文件中,指定bcp文件的路径:

C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\bcp.exe

C:\ Program Files \ Microsoft SQL Server \ Client SDK \ ODBC \ 130 \ Tools \ Binn \ bcp.exe

In arguments specify the bcp arguments to export data from a SQL Server table to a text file named: ssis.bcp

在参数中指定bcp参数,以将数据从SQL Server表导出到名为ssis.bcp的文本文件中

adventureworks.person.person out c:\sql\ssis.bcp -c -T -S WIN-355CHQ0E524

Adventureworks.person.person输出c:\ sql \ ssis.bcp -c -T -S WIN-355CHQ0E524

Your SSIS tasks will look like this:

您的SSIS任务将如下所示:

Right click the tasks and select Execute task:

右键单击任务,然后选择执行任务:

If everything is OK, the task will be like this and a file named ssis.bcp will be created with the data exported:

如果一切正常,任务将是这样,将创建一个名为ssis.bcp的文件,并导出数据:

7. Invoke a batch file in SSIS

7.在SSIS中调用批处理文件

If you are good using the command line, it is common to use SSIS to invoke a batch file that includes several commands and the bcp file. In this new example, we will export data from a SQL Server table to a file including the current date.

如果您擅长使用命令行,通常使用SSIS来调用包含多个命令和bcp文件的批处理文件。 在这个新示例中,我们将数据从SQL Server表导出到包含当前日期的文件中。

The following file named bcpimport.bat

以下文件名为bcpimport.bat

The file will have the following command lines:

该文件将具有以下命令行:


set var=export%date:~-4,4%%date:~-7,2%%date:~-10,2%.bcp
bcp adventureworks.person.person out c:\sql\batfile%var%.bcp -c -T -S WIN-355CHQ0E524

The first line will set the file name in a variable named var, including the current date with the extension bcp. For example, if today is November 6, the file name of the exported data will be batfileexport20170611.bcp.

第一行将在名为var的变量中设置文件名,包括带有扩展名bcp的当前日期。 例如,如果今天是11月6日,则导出数据的文件名将为batfileexport20170611.bcp

To invoke the file in SSIS using the Execute Process task, use the following parameters:

要使用“执行过程”任务在SSIS中调用文件,请使用以下参数:

In executable, we will invoke the cmd which is usually in the

在可执行文件中,我们将调用通常在

c:\Windows\System32\cmd.exe

c:\ Windows \ System32 \ cmd.exe

In arguments, we will use /c bcpimport.bat to invoke the bat file.

在参数中,我们将使用/ c bcpimport.bat调用bat文件。

If everything is fine, file will be created successfully:

如果一切正常,将成功创建文件:

结论 (Conclusions)

Bcp is a powerful tool to import and export data from SQL Server tables to files or vice versa. It is fast, it is also a simple tool that can be easily downloaded. It is possible to export T-SQL queries to a file using bcp.

Bcp是一个强大的工具,可以将数据从SQL Server表导入和导出到文件,反之亦然。 它速度很快,它还是一个易于下载的简单工具。 可以使用bcp将T-SQL查询导出到文件。

SSIS is another alternative that can be used if you need more customized and sophisticated solutions.

如果您需要更多定制的和复杂的解决方案,则可以使用SSIS的另一种选择。

We also learned to invoke bcp in PowerShell. To do that we stored parameters in PowerShell and then we used the Invoke-Expression cmdlet.

我们还学习了如何在PowerShell中调用bcp。 为此,我们将参数存储在PowerShell中,然后使用Invoke-Expression cmdlet。

Finally, we learned how to invoke bcp in SSIS. As we explained, it is not a common practice to invoked bcp in SSIS, but it is possible to do it using the Execute Process Task. However, it is common to invoke .bat files in SSIS and they can contain calls to the bcp. We learned how to use the SSIS Execute Process task to invoke a .bat file.

最后,我们学习了如何在SSIS中调用bcp。 正如我们所解释的,在SSIS中调用bcp是不常见的做法,但是可以使用执行流程任务来执行。 但是,通常在SSIS中调用.bat文件,并且它们可以包含对bcp的调用。 我们学习了如何使用SSIS执行过程任务来调用.bat文件。

翻译自: https://www.sqlshack.com/introduction-bcp-utility-bulk-copy-program-sql-server/

SQL Server中的bcp实用工具(批量复制程序)简介相关推荐

  1. SQL Server中的高可用性(3)----复制 (转载)

    在本系列文章的前两篇对高可用性的意义和单实例下的高可用性做了阐述.但是当随着数据量的增长,以及对RTO和RPO要求的严格,单实例已经无法满足HA/DR方面的要求,因此需要做多实例的高可用性.本文着重对 ...

  2. SQL Server中的数据层应用程序简介

    In this article, I'm going to introduce the data-tier applications in SQL Server. As the official do ...

  3. 在SQL Server中批量复制,导入和导出的技术

    The process of importing or exporting large amounts of data into a SQL Server database, is referred ...

  4. 从TXT文本文档向Sql Server中批量导入数据

    因为工作的需要,近期在做数据的分析和数据的迁移.在做数据迁移的时候需要将原有的数据导入到新建的数据库中.本来这个单纯的数据导入导出是没有什么问题的,但是客户原有的数据全部都是存在.dat文件中的.所以 ...

  5. SQL Server数据库表的基本操作(批量插入、删除、查询数据,删除表中重复数据方法)

    实验名称:数据库表的基本操作与表内数据操作 实验目的: 掌握数据库表创建方法(交互式.T-SQL法) 掌握修改数据库表结构的方法 掌握删除数据库表的方法 掌握交互式EXCEL文件录入数据至数据库表的方 ...

  6. SQL Server中采用BULK INSERT实现大数据量文本文件批量导入

    SQL语句: BULK   INSERT   dbo.table        FROM   'e:/test.tbl '        WITH                (           ...

  7. SQL Server 2005 命令行实用工具

    SQL Server 2005 命令行实用工具 bcp 实用工具 说明:用于在 Microsoft SQL Server 实例和用户指定格式的数据文件之间复制数据. 安装位置:x:"Prog ...

  8. 十步优化SQL Server中的数据访问

    故事开篇:你和你的团队经过不懈努力,终于使网站成功上线,刚开始时,注册用户较少,网站性能表现不错,但随着注册用户的增多,访问速度开始变慢,一些用户开始发来邮件表示抗议,事情变得越来越糟,为了留住用户, ...

  9. 在SQL Server中sqlserver,access,excel之间数据如何使用sql语句直接操作

    所谓的数据传输,其实是指SQLServer访问Access.Excel间的数据. 为什么要考虑到这个问题呢? 由于历史的原因,客户以前的数据很多都是在存入在文本数据库中,如Acess.Excel.Fo ...

最新文章

  1. android 连接服务器
  2. JDBC 实例--JDBC通过工具类DBUtil连接到数据库,让我们不再恐惧操作数据库
  3. 2018.6.8-岁岁年年人不同
  4. ABAP 直接上传图片的函数
  5. extract和extractValue的差别
  6. chi-squared检验_每位数据科学家都必须具备Chi-S方检验统计量:客户流失中的案例研究
  7. Java工具方法——属性拷贝方法:BeanUtils.copyProperties(Object, Object)
  8. Archlinux in VirtualBox
  9. 窗体应用程序:四则运算
  10. vue2.0中的watch和计算属性computed
  11. 机器学习基础算法15-回归实例-线性回归、Ridge回归、LASSO、ElasticNet的高阶参数与过拟合以及TSS>=ESS+RSS代码验证
  12. 《树莓派渗透测试实战》——1.7 树莓派渗透测试场景
  13. python管道_python中管道
  14. 0723函数的递归以及模块的初识
  15. 微信小程序开发及开发中遇到的问题小总结
  16. Spire.Doc使用教程:在Java中使用区域执行邮件合并
  17. 【MySQL 8】MySQL 5.7都即将停只维护了,是时候学习一波MySQL 8了
  18. html 设置表格间距 表格整体大小,css表格单元格间距怎么调整?
  19. 一批脑洞大开的CSS特效
  20. 学习途中的风景 2016年 5月

热门文章

  1. 《C语言程序设计基础I》秋季学习总结
  2. MyBatis(3):SQL映射
  3. Response.End(); 用HttpContext.Current.ApplicationInstance.CompleteRequest 代替
  4. UIKit框架-高级控件Swift版本: 5.UITextView方法/属性详解
  5. 深入入门正则表达式(java) - 1 - 入门基础
  6. 谷歌浏览器一进百度空间就崩溃的临时解决方法
  7. Android开发笔记(一)手势识别
  8. 文件上传 文件大小和类型
  9. CentOS系统安装Java
  10. 养老金上涨后,退休老人每个月6500元的养老金,属于什么水平?