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
Copy file name to clipboardExpand all lines: docs/linux/sql-server-linux-connect-and-query-sqlcmd.md
+105-2Lines changed: 105 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ description:
6
6
author: rothja
7
7
ms.author: jroth
8
8
manager: jhubbard
9
-
ms.date: 10-20-2016
9
+
ms.date: 10-31-2016
10
10
ms.topic: article
11
11
ms.prod: sql-non-specified
12
12
ms.service:
@@ -26,8 +26,111 @@ ms.assetid:
26
26
27
27
---
28
28
# Connect and query SQL Server on Linux (sqlcmd)
29
+
This topic shows how to use [sqlcmd](https://msdn.microsoft.com/library/ms162773.aspx) to connect to SQL Server vNext CTP1 on Linux.
29
30
30
-
This topic provides a walkthrough for using sqlcmd.exe to connect and query SQL Server vNext CTP1 on Linux.
31
+
After successfully connecting, you run a simple Transact-SQL (T-SQL) query to verify communication with the database.
32
+
33
+
## Install the SQL Server command-line tools
34
+
35
+
Sqlcmd is part of the SQL Server command-line tools, which are not installed automatically with SQL Server on Linux. If you have not 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 drop-down list.
36
+
37
+
> [!div class="op_single_selector"]
38
+
-[Ubuntu](sql-server-linux-setup-ubuntu.md#tools)
39
+
-[Red Hat Enterprise Linux](sql-server-linux-setup-red-hat.md#tools)
40
+
-[Docker](sql-server-linux-setup-docker.md#tools)
41
+
42
+
## Connect to SQL Server on Linux
43
+
44
+
The following steps show how to connect to SQL Server vNext on Linux with sqlcmd.
45
+
46
+
1. On your Linux box, open a command terminal.
47
+
48
+
2. Run **sqlcmd** with parameters for your SQL Server instance name (-H), the user name (-U), and the password (-P).
49
+
50
+
The following command connects to the local SQL Server instance (**localhost**) on Linux.
51
+
52
+
sqlcmd -H localhost -U SA -P password
53
+
54
+
> [!TIP]
55
+
> You can omit the password on the command-line to be prompted to enter it manually.
56
+
57
+
If you are connecting to a remote instance, specify the machine name or IP address.
58
+
59
+
sqlcmd -H 192.555.5.555 -U SA -P password
60
+
61
+
> [!TIP]
62
+
> If you get a connection failure, first attempt to diagnose the problem from the error message. Then review the [connection troubleshooting recommendations](sql-server-linux-connect-and-query.md#troubleshoot).
63
+
64
+
## Run sample queries
65
+
66
+
After you connect to your server, you can connect to a database and run a sample query. If you are new to writing queries, see [Writing Transact-SQL Statements](https://msdn.microsoft.com/library/ms365303.aspx).
67
+
68
+
1. Identify a database to use to run a query against. This could be a new database you created in the [Transact-SQL tutorial](https://msdn.microsoft.com/library/ms365303.aspx). Or it could be the **AdventureWorks** sample database that you [downloaded and restored](sql-server-linux-restore-database.md).
69
+
70
+
2. At the sqlcmd prompt, change the context to your target database. The following Transact-SQL statement changes the context to the **AdventureWorks** database.
71
+
72
+
USE AdventureWorks
73
+
74
+
3. On a new line type **GO**, and press enter.
75
+
76
+
GO
77
+
78
+
2. Next, write a Transact-SQL query to select data from one of the tables. You can press enter between lines of your query. The following example selects data from the **Production.Product** table of the **AdventureWorks** database.
79
+
80
+
SELECT TOP 10 Name, ProductNumber
81
+
FROM Production.Product
82
+
ORDER BY Name ASC
83
+
84
+
3. Then type **GO** and press enter.
85
+
86
+
4. The results output to the command window.
87
+
88
+

89
+
90
+
5. Type **exit** and press enter to quit sqlcmd.
91
+
92
+
## Run a Transact-SQL Script
93
+
94
+
You can also use sqlcmd to run Transact-SQL script files. Use the following steps to run a script.
95
+
96
+
1. In a Linux command terminal, use **vi** to create a new script file named sqlscript.sql.
97
+
98
+
vi sqlscript.sql
99
+
100
+
2. In the **vi** editor, press **i** to enter INSERT mode.
101
+
102
+
i
103
+
104
+
3. Enter the same Transact-SQL commands from the previous example.
105
+
106
+
USE AdventureWorks
107
+
GO
108
+
109
+
SELECT TOP 10 Name, ProductNumber
110
+
FROM Production.Product
111
+
ORDER BY Name ASC
112
+
113
+
4. Press the Escape key to exit INSERT mode.
114
+
115
+
5. Press **:x** to save and exit **vi**.
116
+
117
+
:x
118
+
119
+
6. At the prompt, run sqlcmd with the sqlscript.sql file as an input file.
120
+
121
+
sqlcmd -H localhost -U SA -P password -i sqlscript.sql
122
+
123
+
7. You should see 10 rows returned in the output window. Instead of staying at the sqlcmd prompt, you return immediately to the terminal prompt.
124
+
125
+
> [!NOTE]
126
+
> You can also send the output to a file with the **-o** parameter.
In addition to queries, you can use T-SQL statements to create and manage databases.
133
+
134
+
If you're new to T-SQL, see [Tutorial: Writing Transact-SQL Statements](https://msdn.microsoft.com/library/ms365303.aspx) and the [Transact-SQL Reference (Database Engine)](https://msdn.microsoft.com/library/bb510741.aspx).
135
+
33
136
For more information on how to use sqlcmd.exe, see [sqlcmd Utility](https://msdn.microsoft.com/library/ms162773.aspx).
Copy file name to clipboardExpand all lines: docs/linux/sql-server-linux-connect-and-query-ssms.md
+5-4Lines changed: 5 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ ms.assetid:
26
26
27
27
---
28
28
# Connect and query SQL Server on Linux (SSMS)
29
-
This topic shows how to use SQL Server Management Studio (SSMS) to connect to SQL Server vNext CTP1 on Linux. SSMS is a Windows application, so use SSMS when you have a Windows machine that can connect to a remote SQL Server instance on Linux.
29
+
This topic shows how to use [SQL Server Management Studio (SSMS)](https://msdn.microsoft.com/library/mt238290.aspx) to connect to SQL Server vNext CTP1 on Linux. SSMS is a Windows application, so use SSMS when you have a Windows machine that can connect to a remote SQL Server instance on Linux.
30
30
31
31
After successfully connecting, you run a simple Transact-SQL (T-SQL) query to verify communication with the database.
32
32
@@ -73,10 +73,11 @@ After you connect to your server, you can connect to a database and run a sample
73
73
74
74

75
75
76
-
3. In the query window, write a Transact-SQL query to select data from one of the tables. The following example selects data from the **SalesLT.Customer** table of the **AdventureWorks** database.
76
+
3. In the query window, write a Transact-SQL query to select data from one of the tables. The following example selects data from the **Production.Product** table of the **AdventureWorks** database.
Copy file name to clipboardExpand all lines: docs/linux/sql-server-linux-connect-and-query.md
+15-6Lines changed: 15 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,13 +34,22 @@ To connect to SQL Server on Linux, you must use SQL Authentication (username and
34
34
If you are running Linux on an Azure virtual machine, you must also [create a network security group rule](#azure).
35
35
36
36
## Tools
37
-
The following table lists several tools that connect to SQL Server and allow you to run queries.
37
+
The following sections describe several tools that connect to SQL Server and allow you to run queries.
38
38
39
-
| Tool | Description |
40
-
|-----|-----|
41
-
|[sqlcmd](https://msdn.microsoft.com/library/ms162773.aspx)|**Sqlcmd.exe** is a command line tool that can be run from any Windows or Linux machine that has the SQL Server tools installed. It has a basic interface but is good for quickly connecting and running T-SQL commands. <br/>[Connect and query SQL Server on Linux (SqlCmd)](sql-server-linux-connect-and-query-sqlcmd.md)|
42
-
|[Visual Studio Code](https://code.visualstudio.com)|**Visual Studio Code** is a lightweight, cross-platform development tool for writing code. The MSSQL extension provides the ability to execute TSQL commands. This is especially useful in development scenarios where you need to write both application code and database functionality. <br/>[Connect and query SQL Server on Linux (VS Code)](sql-server-linux-connect-and-query-vs-code.md)|
43
-
|[SQL Server Management Studio (SSMS) for Windows](https://msdn.microsoft.com/library/mt238290.aspx)|**SQL Server Management Studio** is a Windows tool for both querying and managing SQL Server instances. This is best for when you are working with both Windows and Linux machines. You can connect SSMS running on Windows to SQL Server running on Linux. The advantage of SSMS is that it provide a graphical user interface for many server and database management tasks. <br/>[Connect and query SQL Server on Linux (SSMS)](sql-server-linux-connect-and-query-ssms.md)|
39
+
### Sqlcmd
40
+
[Sqlcmd](https://msdn.microsoft.com/library/ms162773.aspx) is a command line tool that can be run from any Windows or Linux machine that has the SQL Server tools installed. It has a basic interface but is good for quickly connecting and running T-SQL commands.
41
+
42
+
For a tutorial for Sqlcmd, see [Connect and query SQL Server on Linux (Sqlcmd)](sql-server-linux-connect-and-query-sqlcmd.md).
43
+
44
+
### Visual Studio Code
45
+
[Visual Studio Code (VS Code)](https://code.visualstudio.com) is a lightweight, cross-platform development tool for writing code. The MSSQL extension provides the ability to execute TSQL commands. This is especially useful in development scenarios where you need to write both application code and database functionality.
46
+
47
+
To see a tutorial for VS Code, see [Connect and query SQL Server on Linux (VS Code)](sql-server-linux-connect-and-query-vs-code.md).
48
+
49
+
### SQL Server Management Studio for Windows
50
+
[SQL Server Management Studio (SSMS)](https://msdn.microsoft.com/library/mt238290.aspx) is a Windows tool for both querying and managing SQL Server instances. This is best for when you are working with both Windows and Linux machines. You can connect SSMS running on Windows to SQL Server running on Linux. The advantage of SSMS is that it provide a graphical user interface for many server and database management tasks. <br/>
51
+
52
+
To see a tutorial for SSMS, see [Connect and query SQL Server on Linux (SSMS)](sql-server-linux-connect-and-query-ssms.md).
Copy file name to clipboardExpand all lines: docs/linux/sql-server-linux-setup-docker.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,9 @@ This topic explains how to pull and run the mssql-server Docker image. This imag
70
70
> [!NOTE]
71
71
> The **ACCEPT_EULA** and **SA_PASSWORD** environment variables are required to run the image.
72
72
73
+
## <aid="tools"></a> SQL Server command-line tools
74
+
The Docker image for SQL Server vNext comes with the SQL Server command-line tools and ODBC drivers pre-installed. For more details on these tools, see [Command-line tools and ODBC drivers](sql-server-linux-setup.md#tools).
75
+
73
76
## Next steps
74
77
75
78
After installing SQL Server on Linux, next see [how to connect to the server and run basic Transact-SQL queries](sql-server-linux-connect-and-query.md).
The following steps installs the command-line tools, Microsoft ODBC drivers, and their dependencies. For the details on what gets installed, see [Command-line tools and ODBC drivers](sql-server-linux-setup.md#tools).
Copy file name to clipboardExpand all lines: docs/linux/sql-server-linux-setup-ubuntu.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@ Use the following command to print the installed package name and version:
73
73
74
74
$ dpkg-query -W | grep mssql
75
75
76
-
## Install the tools
76
+
## <aid="tools"></a> Install the tools
77
77
The following steps installs the command-line tools, Microsoft ODBC drivers, and their dependencies. For the details on what gets installed, see [Command-line tools and ODBC drivers](sql-server-linux-setup.md#tools).
0 commit comments