You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo contains command line tools and go packages for working with Microsoft SQL Server, Azure SQL Database, and Azure Synapse.
3
+
This repo contains the `sqlcmd` command line tool and go packages for working with Microsoft SQL Server, Azure SQL Database, and Azure Synapse.
4
+
5
+
## Installation
6
+
7
+
`sqlcmd` is available in package managers for all major platforms.
8
+
9
+
### Windows
10
+
11
+
`sqlcmd` is available via [Winget][], and as a downloadable .msi or .zip from the [releases page][]. The .msi installer is signed with a Microsoft Authenticode certificate.
`sqlcmd` is available via [Homebrew][], and as a downloadable .tar from the [releases page][].
22
+
23
+
#### Homebrew
24
+
25
+
| Install: | Upgrade: |
26
+
| --------------------- | --------------------- |
27
+
|`brew install sqlcmd`|`brew upgrade sqlcmd`|
28
+
29
+
### Linux
30
+
31
+
`sqlcmd` is available via [Linuxbrew][], and as a downloadable .rpm/.deb and .tar from the [releases page][].
32
+
33
+
On Linux, `sqlcmd` is also available through `apt-get`, `yum` and `zypper` package managers. Instructions can be found [here](https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility?view=sql-server-ver16#download-and-install-go-sqlcmd).
34
+
35
+
#### Linuxbrew
36
+
37
+
The Homebrew package manager may be used on Linux and Windows Subsystem for Linux (WSL) 2. Homebrew was formerly referred to as Linuxbrew when running on Linux or WSL.
38
+
39
+
| Install: | Upgrade: |
40
+
| --------------------- | --------------------- |
41
+
|`brew install sqlcmd`|`brew upgrade sqlcmd`|
42
+
43
+
## Use sqlcmd to create local SQL Server and Azure SQL Edge instances
44
+
45
+
Use `sqlcmd` to create SQL Server and Azure SQL Edge instances using a local container runtime (e.g. [Docker][] or [Podman][])
46
+
47
+
### Create SQL Server instance using local container runtime and connect using Azure Data Studio
48
+
49
+
To create a local SQL Server instance with the AdventureWorksLT database restored, query it, and connect to it using Azure Data Studio, run:
Use `sqlcmd --help` to view all the available sub-commands. Use `sqlcmd -?` to view the original ODBC `sqlcmd` flags.
58
+
59
+
### The ~/.sqlcmd/sqlconfig file
60
+
61
+
Each time `sqlcmd create` completes, a new context is created (e.g. mssql, mssql2, mssql3 etc.). A context contains the endpoint and user configuation detail. To switch between contexts, run `sqlcmd config use <context-name>`, to view name of the current context, run `sqlcmd config current-context`, to list all contexts, run `sqlcmd config get-contexts`.
62
+
63
+
To view connection strings (ODBC/ADO.NET/JDBC etc.) for the current context and user & endpoint details for all contexts held in the `~/.sqlcmd/sqlconfig` file:
64
+
65
+
```
66
+
sqlcmd config connection-strings
67
+
sqlcmd config view
68
+
```
69
+
70
+
### Versions
71
+
72
+
To see all version tags to choose from (2017, 2019, 2022 etc.), and install a specific version, run:
73
+
74
+
```
75
+
SET SQLCMD_ACCEPT_EULA=YES
76
+
77
+
sqlcmd create mssql get-tags
78
+
sqlcmd create mssql --tag 2019-latest
79
+
```
80
+
81
+
To stop, start and delete contexts, run the following commands:
82
+
83
+
```
84
+
sqlcmd stop
85
+
sqlcmd start
86
+
sqlcmd delete
87
+
```
88
+
89
+
### Backwards compatibility with ODBC sqlcmd
90
+
91
+
To connect to the current context, and use the original ODBC sqlcmd flags (e.g. -q, -Q, -i, -o etc.), that can be listed with `sqlcmd -?`, run:
92
+
93
+
```
94
+
sqlcmd -q "SELECT @@version"
95
+
sqlcmd
96
+
```
97
+
98
+
If no current context exists, `sqlcmd` (with no connection parameters) reverts to the original ODBC `sqlcmd` behavior of connecting to the default local instance on port 1433 using trusted authentication.
4
99
5
100
## Sqlcmd
6
101
7
-
The `sqlcmd` project aims to be a complete port of the native sqlcmd to the `Go` language, utilizing the [go-mssqldb](https://github.com/microsoft/go-mssqldb) driver. For full documentation of the tool and installation instructions, see https://docs.microsoft.com/sql/tools/go-sqlcmd-utility
102
+
The `sqlcmd` project aims to be a complete port of the original ODBC sqlcmd to the `Go` language, utilizing the [go-mssqldb][] driver. For full documentation of the tool and installation instructions, see [go-sqlcmd-utility][].
8
103
9
-
### Breaking changes
104
+
### Changes in behavior from the ODBC based sqlcmd
10
105
11
-
We will be implementing command line switches and behaviors over time. Several switches and behaviors are expected to change in this implementation.
106
+
The following switches have different behavior in this version of `sqlcmd` compared to the oroignla ODBC based `sqlcmd`.
12
107
13
108
-`-P` switch will be removed. Passwords for SQL authentication can only be provided through these mechanisms:
14
109
@@ -36,15 +131,14 @@ We will be implementing command line switches and behaviors over time. Several s
36
131
- Sqlcmd can now print results using a vertical format. Use the new `-F vertical` command line option to set it. It's also controlled by the `SQLCMDFORMAT` scripting variable.
37
132
38
133
```
39
-
40
134
1> select session_id, client_interface_name, program_name from sys.dm_exec_sessions where session_id=@@spid
41
135
2> go
42
136
session_id 58
43
137
client_interface_name go-mssqldb
44
138
program_name sqlcmd
45
-
46
139
```
47
-
- Sqlcmd now supports shared memory and named pipe transport. Use the appropriate protocol prefix on the server name to force a protocol
140
+
141
+
-`sqlcmd` supports shared memory and named pipe transport. Use the appropriate protocol prefix on the server name to force a protocol:
48
142
*`lpc` for shared memory, only for a localhost. `sqlcmd -S lpc:.`
49
143
*`np` for named pipes. Or use the UNC named pipe path as the server name: `sqlcmd -S \\myserver\pipe\sql\query`
50
144
*`tcp` for tcp `sqlcmd -S tcp:myserver,1234`
@@ -54,16 +148,15 @@ program_name sqlcmd
54
148
1> select net_transport from sys.dm_exec_connections where session_id=@@spid
55
149
2> go
56
150
net_transport Named pipe
57
-
58
151
```
59
152
60
153
### Azure Active Directory Authentication
61
154
62
-
This version of sqlcmd supports a broader range of AAD authentication models, based on the [azidentity package](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity). The implementation relies on an AAD Connector in the [driver](https://github.com/microsoft/go-mssqldb).
155
+
`sqlcmd` supports a broader range of AAD authentication models (over the original ODBC based `sqlcmd`), based on the [azidentity package](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity). The implementation relies on an AAD Connector in the [driver](https://github.com/microsoft/go-mssqldb).
63
156
64
157
#### Command line
65
158
66
-
To use AAD auth, you can use one of two command line switches
159
+
To use AAD auth, you can use one of two command line switches:
67
160
68
161
`-G` is (mostly) compatible with its usage in the prior version of sqlcmd. If a user name and password are provided, it will authenticate using AAD Password authentication. If a user name is provided it will use AAD Interactive authentication which may display a web browser. If no user name or password is provided, it will use a DefaultAzureCredential which attempts to authenticate through a variety of mechanisms.
69
162
@@ -81,7 +174,7 @@ To use AAD auth, you can use one of two command line switches
81
174
82
175
`ActiveDirectoryIntegrated`
83
176
84
-
This method is currently not implemented and will fall back to `ActiveDirectoryDefault`
177
+
This method is currently not implemented and will fall back to `ActiveDirectoryDefault`.
85
178
86
179
`ActiveDirectoryPassword`
87
180
@@ -101,14 +194,14 @@ Use this method when running sqlcmd on an Azure VM that has either a system-assi
101
194
102
195
This method authenticates the provided user name as a service principal id and the password as the client secret for the service principal. Provide a user name in the form `<service principal id>@<tenant id>`. Set `SQLCMDPASSWORD` variable to the client secret. If using a certificate instead of a client secret, set `AZURE_CLIENT_CERTIFICATE_PATH` environment variable to the path of the certificate file.
103
196
104
-
### Environment variables for AAD auth
197
+
####Environment variables for AAD auth
105
198
106
199
Some settings for AAD auth do not have command line inputs, and some environment variables are consumed directly by the `azidentity` package used by `sqlcmd`.
107
200
These environment variables can be set to configure some aspects of AAD auth and to bypass default behaviors. In addition to the variables listed above, the following are sqlcmd-specific and apply to multiple methods.
108
201
109
202
`SQLCMDCLIENTID` - set this to the identifier of an application registered in your AAD which is authorized to authenticate to Azure SQL Database. Applies to `ActiveDirectoryInteractive` and `ActiveDirectoryPassword` methods.
110
203
111
-
## Console colors
204
+
###Console colors
112
205
113
206
Sqlcmd now supports syntax coloring the output of `:list` and the results of TSQL queries when output to the terminal.
114
207
To enable coloring use the `SQLCMDCOLORSCHEME` variable, which can be set as an environment variable or by using `:setvar`. The valid values are the names of styles supported by the [chroma styles](https://github.com/alecthomas/chroma/tree/master/styles) project.
@@ -139,30 +232,22 @@ pkg/sqlcmd is consumable by other hosts. Go docs for the package are forthcoming
139
232
140
233
## Building
141
234
142
-
143
235
```sh
144
-
145
236
build/build
146
-
147
237
```
148
238
149
-
150
239
## Testing
151
240
152
241
The tests rely on SQLCMD scripting variables to provide the connection string parameters. Set SQLCMDSERVER, SQLCMDDATABASE, SQLCMDUSER, SQLCMDPASSWORD variables appropriately then
153
242
154
243
```sh
155
-
156
244
go test ./...
157
-
158
245
```
159
246
160
247
If you are developing on Windows, you can use docker or WSL to run the tests on Linux. `docker run` lets you pass the environment variables. For example, if your code is in `i:\git\go-sqlcmd` you can run tests in a docker container:
161
248
162
249
```cmd
163
-
164
250
docker run -rm -e SQLCMDSERVER=<yourserver> -e SQLCMDUSER=<youruser> -e SQLCMDPASSWORD=<yourpassword> -v i:\git\go-sqlcmd:/go-sqlcmd -w /go-sqlcmd golang:1.16 go test ./...
165
-
166
251
```
167
252
168
253
## Contributing
@@ -187,3 +272,11 @@ trademarks or logos is subject to and must follow
187
272
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
188
273
Any use of third-party trademarks or logos are subject to those third-party's policies.
0 commit comments