Importing/Exporting Data  «Prev  Next»

Lesson 5The bulk copy program (bcp)
Objective Use the bulk copy program.

SQL Bulk Copy Program in SQL - Server

To use the Bulk Copy Program (BCP) on SQL Server 2022 running on Azure Virtual Machines (VMs), follow these steps. BCP is a powerful command-line utility that allows fast import and export of data between a SQL Server table and a data file.
βœ… Overview
BCP is ideal for:
  • Importing large volumes of data into SQL Server from flat files (CSV, TXT).
  • Exporting data from SQL Server to file formats for backup, transfer, or reporting.

When using SQL Server 2022 on Azure VMs, BCP works the same as it does on-premises, but with extra considerations for:
  • Network security
  • Storage locations (e.g., local disk or mounted Azure File Share)

🧱 Prerequisites
  1. Install BCP Utility (if not already available):
    • Included with SQL Server tools or MS Command Line Utilities.
    • Verify with:
      bcp -v
      
  2. Ensure the SQL Server is Accessible:
    • On your Azure VM, SQL Server must be listening on TCP port 1433.
    • Add an inbound NSG rule to allow traffic (if testing externally).
  3. Authentication:
    • Use SQL authentication (-U and -P) or Windows authentication (-T if the VM is domain-joined).
  4. Permissions:
    • Ensure the SQL login has BULK INSERT and table-level INSERT or SELECT permissions.

πŸ“₯ Import Example (Data File β†’ SQL Table)

bcp YourDatabase.dbo.YourTable in "C:\data\importfile.csv" ^
   -S localhost -U sa -P YourPassword ^
   -c -t, -r\n

Flags Explained:
  • YourDatabase.dbo.YourTable: Full name of the target table.
  • in: Direction is import.
  • -S: SQL Server instance (use localhost or public IP).
  • -U / -P: SQL login credentials.
  • -c: Character data type (default format).
  • -t,: Field terminator is a comma (CSV).
  • -r\n: Row terminator is newline.

πŸ“€ Export Example (SQL Table β†’ Data File)
bcp YourDatabase.dbo.YourTable out "C:\data\exportfile.csv" ^
   -S localhost -U sa -P YourPassword ^
   -c -t, -r\n

This will export the table’s data into a CSV file.*
πŸ” Security Tip for Azure VMs
  • Avoid exposing your SQL Server to the public internet.
  • If you must connect remotely, use Azure Bastion, VPN, or Just-in-Time VM access.
  • Always encrypt your BCP file if it contains sensitive data.

πŸ’‘ Advanced Use
* Export using a query:
  bcp "SELECT * FROM YourDatabase.dbo.YourTable WHERE status = 'active'" queryout "C:\data\filtered.csv" ^
     -S localhost -U sa -P YourPassword ^
     -c -t, -r\n
  

* Use format files for complex schemas:
  bcp YourDatabase.dbo.YourTable in "C:\data\importfile.csv" -f "C:\data\formatfile.fmt" ^
     -S localhost -U sa -P YourPassword
  


"Azure CLI example" to automate the Bulk Copy Process

Here's a complete Azure CLI script to automate the BCP (Bulk Copy Program) import process into SQL Server 2022 running on an Azure VM. This script includes:
  • Connecting to the Azure VM
  • Uploading a CSV file to the VM
  • Executing the bcp command remotely using a run command
  • Cleanup steps (optional)

βœ… Azure CLI Script to Automate BCP Import
πŸ”§ Prerequisites
  • SQL Server 2022 is installed and listening on localhost on your Azure VM
  • The VM is running Windows OS
  • You have a CSV file ready for upload (e.g., customers.csv)
  • The SQL Server login (e.g., sa) and password are known
  • bcp is available in the system path

πŸ“œ Script
#!/bin/bash

# Variables
RESOURCE_GROUP="MyResourceGroup"
VM_NAME="MySqlVm"
VM_ADMIN_USER="azureuser"
LOCAL_DATA_FILE="customers.csv"
REMOTE_DATA_FILE="C:\\data\\customers.csv"
SQL_DATABASE="SalesDB"
SQL_TABLE="dbo.Customers"
SQL_USER="sa"
SQL_PASSWORD="YourStrongPassword123"

  • Step 1: Upload CSV to the Azure VM
    az vm run-command invoke \
      --resource-group "$RESOURCE_GROUP" \
      --name "$VM_NAME" \
      --command-id 'RunPowerShellScript' \
      --scripts "New-Item -Path 'C:\\data' -ItemType Directory -Force"
    
    az vm run-command invoke \
      --resource-group "$RESOURCE_GROUP" \
      --name "$VM_NAME" \
      --command-id 'RunPowerShellScript' \
      --scripts "Set-Content -Path '$REMOTE_DATA_FILE' -Value ''"  # Initialize empty file
    
    az vm extension set \
      --publisher Microsoft.Compute \
      --name CustomScriptExtension \
      --resource-group "$RESOURCE_GROUP" \
      --vm-name "$VM_NAME" \
      --settings "{\"fileUris\": [\"https://<your-storage-account>.blob.core.windows.net/<your-container>/$LOCAL_DATA_FILE\"], \"commandToExecute\": \"powershell -ExecutionPolicy Unrestricted -Command Move-Item -Path $LOCAL_DATA_FILE -Destination $REMOTE_DATA_FILE\"}"
    
  • Step 2: Run the BCP import command on the VM
    az vm run-command invoke \
      --resource-group "$RESOURCE_GROUP" \
      --name "$VM_NAME" \
      --command-id 'RunPowerShellScript' \
      --scripts "bcp $SQL_DATABASE.$SQL_TABLE in \"$REMOTE_DATA_FILE\" -S localhost -U $SQL_USER -P $SQL_PASSWORD -c -t, -r\\n"
    
    echo "βœ… BCP import executed."
    


πŸ“Œ Notes:
  • Replace the blob URL with your real Azure Blob Storage file path or implement az storage blob upload if you're using azcopy.
  • CustomScriptExtension assumes you are moving files from Azure Blob to the VM. You can also SCP the file directly if the VM has SSH access.
  • You can wrap this script inside a GitHub Actions workflow or Azure DevOps pipeline for CI/CD.

πŸ” Security Considerations:
* Store passwords in Azure Key Vault and retrieve them using:
  SQL_PASSWORD=$(az keyvault secret show --vault-name "MyVault" --name "SqlPassword" --query value -o tsv)
  
Like the bulk insert statement, the bulk copy program (bcp) can be used to load data from a file into a table. However, unlike the bulk insert statement, bcp can also be used to extract data from a table. The parameters for the bcp program are similar to the bulk insert statement options. The following MouseOver illustrates the syntax of the bulk copy program.

(BCP) Bulk Copy Program in SQL Server

bulk-copy consisting of dbtable, query, in | out | queryout | format
  1. dbtable – The permanent table, global temporary table, or view to access.
  2. query – bcp can extract data from a query, the query must be enclosed in double quotes(“query”).
  3. in | out | queryout | format – Specifies what the bcp is being used for.
  4. Datafile – The file to export/import; you must specify the datafile.
  5. -m maxerrors – The maximum number of errors before the bcp is aborted; this parameter defaults to 10.
  6. -f formatfile – Specifies the file that contains information; defaults to no format file.
  7. -e errfile – A file that all records that generate errors will be copied to. If you do not specify a error file, SQL Server will print error information to the screen, but will not tell you what row caused the problem; defaults to no errorfiles.
  8. -F firstrow – Specifies the first row from the file to load, or the first row from the table to export. Defaults to the first row.
  9. -L lastrow – Specifies the last row to load, or the last row to export; defaults to all the rows.
  10. -b batchsize – Specifies how many rows to import as one transaction; defaults to all the file
  11. -n – Data is stored as SQL Server’s native type; defaults to not being set.
  12. -c – Data is stored as character; defaults to not being set.
  13. -w – Data is unicode character; defaults to not being set.
  14. -N – Non-text data is native format; defaults to not being set.
  15. -q – Quoted identifier specifies that the dbtable string will either contain quotes or brackets to delimit object names; defaults tonot being set.
  16. -C code page – Specifies the code page that is used to store character data; defaults to SQL Servers standard code page
  17. -t field terminator – Specifies the field terminator; default is a tab (\t)
  18. -r row terminator – Specifies the row terminator; default is a newline (\n)
  19. -i inputfile – Allows you to specify a file that contains the answers to questions describing the format of the bcp; defaults to noinput file.
  20. -o outfile – Allows you to specify a file in which bcp will store its output; defaults to no output file
  21. -a packetsize – Allows you to specify how many bytes will be sent between SQL Server and bcp in a newtwork packet; defaults to SQL Server’s configured value.
  22. -S server name – The SQL Server to connect with; defaults to the local server.
  23. -U username – The SQL Server standard security account to use when connecting to SQL Server; defaults to your Windows Login name.
  24. -P password – The password to use when connecting to SQL Server. If a password is not provided, bcp will prompt you for one; there is no default.
  25. -T – bcp will connect using a trusted connection, using integrated security; defaults to not being set.
  26. -v – bcp will display its version; defaults to not being set.
  27. -R – The client settings for date, time, and currency formats will be used; defaults to not being set.
  28. -k – SQL will not apply default values for columns that are not loaded as part of the bcp operation; defaults to not being set.
  29. -E – SQL will not generate new identity values for tables containing identity types; defaults to not being set.
  30. -h – "load hints" – Allows you to control the behavior of bcp by specifying one of the following – CHECK_CONSTRAINTS, KILLOBYTES_PER_BATCH, ORDER, ROWS_PER_BATCH, or TABLOCK. These all behave the same as the BULK INSERT statements options.

bulk-copy consisting of dbtable, query, in | out | queryout | format


When performing bulk copies, you should first know the following information:
  1. When using a query, you can specify any valid SQL statement.
  2. If you are using a query and it produces multiple result sets (either from calling a stored procedure, or having a compute by[1] clause), the bcp will only extract the first result set.
  3. If a data file is missing, the first parameter after the direction ( IN, OUT, FORMAT, QUERYOUT) is treated as the data file name.
  4. The –E parameter has no effect if the file does not contain data for the identity column.
  5. If –n, -c, -w, -6 , -N or –f are not specified, bcp will prompt you for format information about each column.
    If one of –n, -c, -w, -6 , -N or –f is specified, a \r will be used as the row terminator and a \t will be used as a field terminator.
  6. In = Import data; out = export data; queryout = you are using a query to export data; format = generate a format file without extracting data--this must be used with the –f option.

Example

bcp Sales.employee in empl.data 
-S WesternRegion –T -c

  1. The example above loads data from the file empl.data into the Sales database.
  2. The employee table is on the WesternRegion database server and uses integrated security.
  3. This file has data stored in character format, with a tab between fields and a carriage return at the end of each record.
While bcp allows you to transfer data between SQL Server and files, often times you will want to move data between other data sources. The next lesson will introduce you to the basics of the data transformation services.
[1] Compute by: A TSQL extension that allows you to have summary rows intermixed with detail rows.

SEMrush Software Target 5SEMrush Software Banner 5