Skip to content

Commit bd7b75c

Browse files
authored
Merge pull request #20323 from David-Engel/token
Add example command line for obtaining an access token
2 parents e9eee7b + b55b451 commit bd7b75c

2 files changed

Lines changed: 354 additions & 301 deletions

File tree

Lines changed: 134 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: "Connecting with bcp"
3-
description: "Learn how to use the bcp utility with the Microsoft ODBC Driver for SQL Server on Linux and macOS."
2+
title: Connecting with bcp
3+
description: Learn the options and commands available in the bcp utility, available in the mssql-tools package on Linux and macOS.
44
ms.custom: ""
5-
ms.date: "07/30/2021"
5+
ms.date: 09/30/2021
66
ms.prod: sql
77
ms.prod_service: connectivity
88
ms.reviewer: ""
@@ -15,142 +15,149 @@ author: David-Engel
1515
ms.author: v-daenge
1616
---
1717
# Connecting with bcp
18+
1819
[!INCLUDE[Driver_ODBC_Download](../../../includes/driver_odbc_download.md)]
1920

20-
The [bcp](../../../tools/bcp-utility.md) utility is available with the [!INCLUDE[msCoName](../../../includes/msconame_md.md)] ODBC Driver for [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] on Linux and macOS. This page documents the differences from the Windows version of `bcp`.
21-
22-
- The field terminator is a tab ("\t").
23-
24-
- The line terminator is a newline ("\n").
25-
26-
- Character mode is the preferred format for `bcp` format files and data files that do not contain extended characters.
27-
28-
> [!NOTE]
29-
> A backslash '\\' on a command-line argument must either be quoted or escaped. For example, to specify a newline as a custom row terminator, you must use one of the following mechanisms:
30-
>
31-
> - -r\\\n
32-
> - -r"\n"
33-
> - -r'\n'
34-
35-
The following example is a command invocation of `bcp` to copy table rows to a text file:
36-
37-
```
38-
bcp AdventureWorks2008R2.Person.Address out test.dat -Usa -Pxxxx -Sxxx.xxx.xxx.xxx
39-
```
40-
41-
## Available Options
42-
In the current release, the following syntax and options are available:
21+
The [bcp](../../../tools/bcp-utility.md) utility is available with the [!INCLUDE[msCoName](../../../includes/msconame_md.md)] ODBC Driver for [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] on Linux and macOS. This page documents the differences from the [Windows version of bcp](../../../tools/bcp-utility.md).
4322

44-
[_database_**.**]_schema_**.**_table_ **in** _data\_file_ | **out** _data\_file_
23+
- The field terminator is a tab ("\t").
4524

46-
- -a *packet_size*
47-
Specifies the number of bytes, per network packet, sent to and from the server.
48-
49-
- -b *batch_size*
50-
Specifies the number of rows per batch of imported data.
51-
52-
- -c
53-
Uses a character data type.
54-
55-
- -d *database_name*
56-
Specifies the database to connect to.
57-
58-
- -D
59-
Causes the value passed to the `bcp` -S option to be interpreted as a data source name (DSN). For more information, see "DSN Support in sqlcmd and bcp" in [Connecting with sqlcmd](connecting-with-sqlcmd.md).
60-
61-
- -e *error_file*
62-
Specifies the full path of an error file used to store any rows that the `bcp` utility cannot transfer from the file to the database.
63-
64-
- -E
65-
Uses an identity value or values in the imported data file for the identity column.
66-
67-
- -f *format_file*
68-
Specifies the full path of a format file.
69-
70-
- -F *first_row*
71-
Specifies the number of the first row to export from a table or import from a data file.
25+
- The line terminator is a newline ("\n").
7226

73-
- -G
74-
This switch is used by the client when connecting to Azure SQL Database or Azure Synapse Analytics to specify that the user be authenticated using Azure Active Directory authentication. It can be combined with just the -P option to use access token authentication (v17.8+). The -G switch requires at least bcp version 17.6. To determine your version, execute bcp -v.
27+
- Character mode is the preferred format for `bcp` format files and data files that don't contain extended characters.
28+
29+
> [!NOTE]
30+
> A backslash '\\' on a command-line argument must either be quoted or escaped. For example, to specify a newline as a custom row terminator, you must use one of the following mechanisms:
31+
>
32+
> - -r\\\n
33+
> - -r"\n"
34+
> - -r'\n'
35+
36+
The following example is a command invocation of `bcp` to copy table rows to a text file:
37+
38+
```bash
39+
bcp AdventureWorks2008R2.Person.Address out test.dat -Usa -Pxxxx -Sxxx.xxx.xxx.xxx
40+
```
7541

42+
## Available options
43+
44+
In the current release, the following syntax and options are available:
45+
46+
[_database_**.**]_schema_**.**_table_ **in** *data_file* | **out** *data_file*
47+
48+
**-a** *packet_size*
49+
Specifies the number of bytes, per network packet, sent to and from the server.
50+
51+
**-b** *batch_size*
52+
Specifies the number of rows per batch of imported data.
53+
54+
**-c**
55+
Uses a character data type.
56+
57+
**-d** *database_name*
58+
Specifies the database to connect to.
59+
60+
**-D**
61+
Causes the value passed to the `bcp` -S option to be interpreted as a data source name (DSN). For more information, see "DSN Support in sqlcmd and bcp" in [Connecting with sqlcmd](connecting-with-sqlcmd.md).
62+
63+
**-e** *error_file*
64+
Specifies the full path of an error file used to store any rows that the `bcp` utility can't transfer from the file to the database.
65+
66+
**-E**
67+
Uses an identity value or values in the imported data file for the identity column.
68+
69+
**-f** *format_file*
70+
Specifies the full path of a format file.
71+
72+
**-F** *first_row*
73+
Specifies the number of the first row to export from a table or import from a data file.
74+
75+
**-G**
76+
This switch is used by the client when connecting to Azure SQL Database or Azure Synapse Analytics to specify that the user be authenticated using Azure Active Directory authentication. It can be combined with just the -P option to use access token authentication (v17.8+). The -G switch requires at least bcp version 17.6. To determine your version, execute bcp -v.
7677

7778
> [!IMPORTANT]
7879
> The `-G` option only applies to Azure SQL Database and Azure Synapse Analytics.
7980
> AAD Interactive Authentication is not currently supported on Linux or macOS. AAD Integrated Authentication requires [Microsoft ODBC Driver 17 for SQL Server](../download-odbc-driver-for-sql-server.md) version 17.6.1 or higher and a properly [configured Kerberos environment](using-integrated-authentication.md#configure-kerberos).
8081
81-
- -k
82-
Specifies that empty columns should retain a null value during the operation, rather than have any default values for the columns inserted.
83-
84-
- -l
85-
Specifies a login timeout. The -l option specifies the number of seconds before a login to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] times out when you try to connect to a server. The default login timeout is 15 seconds. The login timeout must be a number between 0 and 65534. If the value supplied is not numeric or does not fall into that range, `bcp` generates an error message. A value of 0 specifies an infinite timeout.
86-
87-
- -L *last_row*
88-
Specifies the number of the last row to export from a table or import from a data file.
89-
90-
- -m *max_errors*
91-
Specifies the maximum number of syntax errors that can occur before the `bcp` operation is canceled.
92-
93-
- -n
94-
Uses the native (database) data types of the data to perform the bulk-copy operation.
95-
96-
- -P *password*
97-
Specifies the password for the login ID. When used with the -G option without -U, specifies a file that contains an access token (v17.8+). The token file should be in UTF-16 format.
82+
**-k**
83+
Specifies that empty columns should keep a null value during the operation, rather than have any default values for the columns inserted.
9884

99-
100-
- -q
101-
Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the `bcp` utility and an instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
102-
103-
- -r *row_terminator*
104-
Specifies the row terminator.
105-
106-
- -R
107-
Specifies that currency, date, and time data is bulk copied into [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] using the regional format defined for the locale setting of the client computer.
108-
109-
- -S *server*
110-
Specifies the name of the [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] instance to connect to, or if -D is used, a DSN.
111-
112-
- -t *field_terminator*
113-
Specifies the field terminator.
114-
115-
- -T
116-
Specifies that the `bcp` utility connect to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] with a trusted connection (integrated security).
117-
118-
- -U *login_id*
119-
Specifies the login ID used to connect to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
120-
121-
- -v
122-
Reports the `bcp` utility version number and copyright.
123-
124-
- -w
125-
Uses Unicode characters to perform the bulk copy operation.
126-
127-
In this release, Latin-1 and UTF-16 characters are supported.
128-
129-
## Unavailable Options
130-
In the current release, the following syntax and options are not available:
85+
**-l**
86+
Specifies a login timeout. The -l option specifies the number of seconds before a login to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] times out when you try to connect to a server. The default login timeout is 15 seconds. The login timeout must be a number between 0 and 65534. If the value supplied isn't numeric or doesn't fall into that range, `bcp` generates an error message. A value of 0 specifies an infinite timeout.
13187

132-
- -C
133-
Specifies the code page of the data in the data file.
134-
135-
- -h *hint*
136-
Specifies the hint or hints used during a bulk import of data into a table or view.
88+
**-L** *last_row*
89+
Specifies the number of the last row to export from a table or import from a data file.
13790

138-
- -i *input_file*
139-
Specifies the name of a response file.
140-
141-
- -N
142-
Uses the native (database) data types of the data for noncharacter data, and Unicode characters for character data.
143-
144-
- -o *output_file*
145-
Specifies the name of a file that receives output redirected from the command prompt.
146-
147-
- -V (80 | 90 | 100)
148-
Uses data types from an earlier version of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
149-
150-
- -x
151-
Used with the format and -f format_file options, generates an XML-based format file instead of the default non-XML format file.
152-
153-
## See Also
91+
**-m** *max_errors*
92+
Specifies the maximum number of syntax errors that can occur before the `bcp` operation is canceled.
93+
94+
**-n**
95+
Uses the native (database) data types of the data to do the bulk-copy operation.
96+
97+
**-P** *password*
98+
Specifies the password for the login ID. When used with the -G option without -U, specifies a file that contains an access token (v17.8+). The token file should be in UTF-16LE (no BOM) format.
99+
100+
Access tokens can be obtained via various methods. It's important to ensure the access token is correct byte-for-byte, as it will be sent as-is. Below is an example command that obtains an access token. The command uses the Azure CLI and Linux commands and saves it to a file in the proper format. If your system or terminal's default encoding isn't ASCII or UTF-8, you may need to adjust the `iconv` options. Be sure to carefully secure the resulting file and delete it when it's no longer required.
101+
102+
```bash
103+
az account get-access-token --resource https://database.windows.net --output tsv | cut -f 1 | tr -d '\n' | iconv -f ascii -t UTF-16LE > /tmp/tokenFile
104+
```
105+
106+
**-q**
107+
Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the `bcp` utility and an instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
108+
109+
**-r** *row_terminator*
110+
Specifies the row terminator.
111+
112+
**-R**
113+
Specifies that currency, date, and time data is bulk copied into [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] using the regional format defined for the locale setting of the client computer.
114+
115+
**-S** *server*
116+
Specifies the name of the [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] instance to connect to, or if -D is used, a DSN.
117+
118+
**-t** *field_terminator*
119+
Specifies the field terminator.
120+
121+
**-T**
122+
Specifies that the `bcp` utility connect to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] with a trusted connection (integrated security).
123+
124+
**-U** *login_id*
125+
Specifies the login ID used to connect to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
126+
127+
**-v**
128+
Reports the `bcp` utility version number and copyright.
129+
130+
**-w**
131+
Uses Unicode characters to do the bulk copy operation.
132+
133+
In this release, Latin-1 and UTF-16 characters are supported.
134+
135+
## Unavailable options
136+
137+
In the current release, the following syntax and options aren't available:
138+
139+
**-C**
140+
Specifies the code page of the data in the data file.
141+
142+
**-h** *hint*
143+
Specifies the hint or hints used during a bulk import of data into a table or view.
144+
145+
**-i** *input_file*
146+
Specifies the name of a response file.
147+
148+
**-N**
149+
Uses the native (database) data types of the data for noncharacter data, and Unicode characters for character data.
150+
151+
**-o** *output_file*
152+
Specifies the name of a file that receives output redirected from the command prompt.
153+
154+
**-V** (80 | 90 | 100)
155+
Uses data types from an earlier version of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)].
156+
157+
**-x**
158+
Used with the format and -f format_file options, generates an XML-based format file instead of the default non-XML format file.
159+
160+
## See also
154161

155-
[Connecting with **sqlcmd**](connecting-with-sqlcmd.md)
156-
[Release Notes](release-notes-tools.md)
162+
[Connecting with sqlcmd](connecting-with-sqlcmd.md)
163+
[Release notes](release-notes-tools.md)

0 commit comments

Comments
 (0)