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.
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:
Drive: \\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\bcp.exe
In this article we will learn how to:
- Get the bcp arguments
- Get the version
- Export data from a SQL Server table to a file
- Export data from a SQL Server query to a file
- Run bcp using PowerShell
- Run bcp on SSIS
- Invoke a batch file in SSIS
Requirements
- SQL Server installed
- bcp installed
- The Adventureworks database installed
- SSIS Installed
- SSDT installed
Get started
1. Get the bcp arguments
In the command line, write bcp. This will show you the possible arguments that can be used:
bcp /? Is the same than bcp. It shows you the possible parameters:
2. Get the version
You can get the version of the bcp using the -v argument:
Here you have a list of versions of bcp:
bcp version | SQL Server |
14.0.X.X | SQL Server 2017 |
13.0.X.X | SQL Server 2016 |
12.0.X.X | SQL Server 2014 |
11.0.X.X | SQL Server 2012 |
10.0.X.X | SQL Server 2008 |
9.0.X.X | SQL Server 2005 |
8.0.X.X | SQL Server 2000 |
7.0.X.X | SQL Server 7 |
3. Export data from a SQL Server table to a file
To export data from a SQL Server table or view to a file, you can use the bcp command.
The following example exports the table person.person from the adventureworks database to a file named dimcustomer.bcp
bcp adventureworks.person.person out 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.
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
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:
bcp adventureworks.person.person out c:\sql\dimcustomer.bcp -c -T -S WIN-355CHQ0E524
The result displayed is the following:
5. Run bcp using PowerShell
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.
In PowerShell create variables to store the database name, schema, table and output path:
1 2 3 4 5 6 |
$db = "adventureworks" $schema = "person" $table = "person" $path = "C:\sql\powershell.txt" |
Store the bcp command line with the parameters in another parameter:
1 2 3 |
$psCommand = "bcp $($db).$($schema).$($table) out $path -T -c" |
Use the Invoke-Expression to call the variable with the bcp commands:
1 2 |
Invoke-Expression $psCommand |
PowerShell will import the files in the powershell.txt file:
6. Run bcp on SSIS
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.
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.
Open the SSDT and create a New Project.
In projects select Integration Services Project:
Drag and drop the Execute Process Task to the design pane:
In executable, specify the path of the bcp file:
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
adventureworks.person.person out c:\sql\ssis.bcp -c -T -S WIN-355CHQ0E524
Your SSIS tasks will look like this:
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:
7. Invoke a batch file in 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.
The following file named bcpimport.bat
The file will have the following command lines:
1 2 3 4 |
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.
To invoke the file in SSIS using the Execute Process task, use the following parameters:
In executable, we will invoke the cmd which is usually in the
c:\Windows\System32\cmd.exe
In arguments, we will use /c bcpimport.bat to invoke the bat file.
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.
SSIS is another alternative that can be used if you need more customized and sophisticated solutions.
We also learned to invoke bcp in PowerShell. To do that we stored parameters in PowerShell and then we used the 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.
- PostgreSQL tutorial to create a user - November 12, 2023
- PostgreSQL Tutorial for beginners - April 6, 2023
- PSQL stored procedures overview and examples - February 14, 2023