--- title: Bulk copy data to SQL Server on Linux description: This article describes the bcp utility. Use bcp to import large numbers of rows into SQL Server tables or to export data from SQL Server tables into data files. author: rwestMSFT ms.author: randolphwest ms.date: 11/16/2023 ms.service: sql ms.subservice: linux ms.topic: conceptual ms.custom: - linux-related-content --- # Bulk copy data with bcp to SQL Server on Linux [!INCLUDE [SQL Server - Linux](../includes/applies-to-version/sql-linux.md)] This article shows how to use the [bcp utility](../tools/bcp-utility.md) to bulk copy data between an instance of SQL Server on Linux and a data file in a user-specified format. You can use **bcp** to import large numbers of rows into SQL Server tables or to export data from SQL Server tables into data files. Except when used with the queryout option, **bcp** requires no knowledge of Transact-SQL. The **bcp** command-line utility works with Microsoft SQL Server running on-premises or in the cloud, on Linux, Windows or Docker and Azure SQL Database and Azure Synapse Analytics. This article shows you how to: - Import data into a table using the `bcp in` command - Export data from a table using the `bcp out` command ## Install the SQL Server command-line tools **bcp** is part of the SQL Server command-line tools, which aren't installed automatically with SQL Server on Linux. If you haven't already installed the SQL Server command-line tools on your Linux machine, you must install them. For more information on how to install the tools, select your Linux distribution from the following list: - [Red Hat Enterprise Linux (RHEL)](sql-server-linux-setup-tools.md#RHEL) - [Ubuntu](sql-server-linux-setup-tools.md#ubuntu) - [SUSE Linux Enterprise Server (SLES)](sql-server-linux-setup-tools.md#SLES) ## Import data with bcp In this tutorial, you create a sample database and table on the local SQL Server instance (`localhost`) and then use **bcp** to load into the sample table from a text file on disk. ### Create a sample database and table Let's start by creating a sample database with a simple table that is used in the rest of this tutorial. 1. On your Linux box, open a command terminal. 1. Copy and paste the following commands into the terminal window. These commands use the **sqlcmd** command-line utility to create a sample database (`BcpSampleDB`) and a table (`TestEmployees`) on the local SQL Server instance (`localhost`). Remember to replace the `username` and `` as necessary before running the commands. Create the database `BcpSampleDB`: ```bash sqlcmd -S localhost -U sa -P -Q "CREATE DATABASE BcpSampleDB;" ``` Create the table `TestEmployees` in the database `BcpSampleDB`: ```bash sqlcmd -S localhost -U sa -P -d BcpSampleDB -Q "CREATE TABLE TestEmployees (Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, Name NVARCHAR(50), Location NVARCHAR(50));" ``` ### Create the source data file Copy and paste the following command into your terminal window. We use the built-in `cat` command to create a sample text data file with three records save the file in your home directory as `~/test_data.txt`. The fields in the records are delimited by a comma. ```bash cat > ~/test_data.txt << EOF 1,Jared,Australia 2,Nikita,India 3,Tom,Germany EOF ``` You can verify that the data file was created correctly by running the following command in your terminal window: ```bash cat ~/test_data.txt ``` This should display the following in your terminal window: ```output 1,Jared,Australia 2,Nikita,India 3,Tom,Germany ``` ### Import data from the source data file Copy and paste the following commands into the terminal window. This command uses **bcp** to connect to the local SQL Server instance (`localhost`) and import the data from the data file (`~/test_data.txt`) into the table (`TestEmployees`) in the database (`BcpSampleDB`). Remember to replace the username and `` as necessary before running the commands. ```bash bcp TestEmployees in ~/test_data.txt -S localhost -U sa -P -d BcpSampleDB -c -t ',' ``` Here's a brief overview of the command-line parameters we used with **bcp** in this example: - `-S`: specifies the instance of SQL Server to which to connect - `-U`: specifies the login ID used to connect to SQL Server - `-P`: specifies the password for the login ID - `-d`: specifies the database to connect to - `-c`: performs operations using a character data type - `-t`: specifies the field terminator. We are using `comma` as the field terminator for the records in our data file > [!NOTE] > We aren't specifying a custom row terminator in this example. Rows in the text data file were correctly terminated with `newline` when we used the `cat` command to create the data file earlier. You can verify that the data was successfully imported by running the following command in your terminal window. Remember to replace the `username` and `` as necessary before running the command. ```bash sqlcmd -S localhost -d BcpSampleDB -U sa -P -I -Q "SELECT * FROM TestEmployees;" ``` This should display the following results: ```output Id Name Location ----------- ------------------- ------------------- 1 Jared Australia 2 Nikita India 3 Tom Germany ``` ## Export data with bcp In this tutorial, you use **bcp** to export data from the sample table we created earlier to a new data file. Copy and paste the following commands into the terminal window. These commands use the **bcp** command-line utility to export data from the table `TestEmployees` in the database `BcpSampleDB` to a new data file called `~/test_export.txt`. Remember to replace the username and `` as necessary before running the command. ```bash bcp TestEmployees out ~/test_export.txt -S localhost -U sa -P -d BcpSampleDB -c -t ',' ``` You can verify that the data was exported correctly by running the following command in your terminal window: ```bash cat ~/test_export.txt ``` This should display the following in your terminal window: ```output 1,Jared,Australia 2,Nikita,India 3,Tom,Germany ``` ## Related content - [bcp utility](../tools/bcp-utility.md) - [Specify compatibility data formats when using bcp (SQL Server)](../relational-databases/import-export/specify-data-formats-for-compatibility-when-using-bcp-sql-server.md) - [Use BULK INSERT or OPENROWSET(BULK...) to import data to SQL Server](../relational-databases/import-export/import-bulk-data-by-using-bulk-insert-or-openrowset-bulk-sql-server.md) - [BULK INSERT (Transact-SQL)](../t-sql/statements/bulk-insert-transact-sql.md)