| title | Use the sqlcmd command-line utility on Linux - SQL Server vNext CTP1 | Microsoft Docs |
|---|---|
| description | This tutorial shows how to run sqlcmd on Linux to run Transact-SQL queries. |
| author | rothja |
| ms.author | jroth |
| manager | jhubbard |
| ms.date | 11/15/2016 |
| ms.topic | article |
| ms.prod | sql-linux |
| ms.technology | database-engine |
| ms.assetid | 9e6c1ae1-59a4-4589-b839-18d6a52f2676 |
This topic provides connection requirements and guidance for SQL Server vNext CTP1 running on Linux. In most cases, the connection requirements and processes do not differ across platforms. This topic approaches the subject in the context of Linux and then points to other resources.
This topic shows how to use sqlcmd to connect to SQL Server vNext on Linux. After successfully connecting, run a simple Transact-SQL (T-SQL) query to verify communication with the database.
Tip
Sqlcmd is just one tool for connecting to SQL Server to run queries and perform management and development tasks. For other tools such as SQL Server Management Studio and Visual Studio Code, see the Develop and Manage areas.
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, follow the instructions for your Linux distribution:
To connect to SQL Server on Linux, you must use SQL Authentication (username and password). To connect remotely, you must ensure that the port SQL Server listens on is open. By default, SQL Server listens on TCP port 1433. Depending on your Linux distribution and configuration, you might have to open this port in the firewall.
The following steps show how to connect to SQL Server vNext on Linux with sqlcmd.
-
On your Linux box, open a command terminal.
-
Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P).
The following command connects to the local SQL Server instance (localhost) on Linux.
sqlcmd -S localhost -U SA -P '<YourPassword>'[!TIP] You can omit the password on the command-line to be prompted to enter it.
To connect to a remote instance, specify the machine name or IP address for the -S parameter.
sqlcmd -S 192.555.5.555 -U SA -P '<YourPassword>'[!TIP] If you get a connection failure, first attempt to diagnose the problem from the error message. Then review the connection troubleshooting recommendations.
After you connect to your server, run some queries. If you are new to writing queries, see Writing Transact-SQL Statements.
For example, this query returns the name of all of the databases.
SELECT Name from sys.Databases;
GOThis query creates a database using the SQL Server default settings.
CREATE DATABASE testdb;
GOUse the database:
USE testdb;
GOCreate a table in the current database:
CREATE TABLE inventory (id INT, name NVARCHAR(50), quantity INT);
GOInsert data into the new table:
INSERT INTO inventory VALUES (1, 'banana', 150);
INSERT INTO inventory VALUES (2, 'orange', 154);
GOSelect from the table:
SELECT * FROM inventory WHERE quantity > 152;
GOTo end your sqlcmd session, type QUIT.
QUITIn this walk-through you connected to SQL Server, and created and populated a database. For more information on how to use sqlcmd.exe, see sqlcmd Utility.
If you're new to T-SQL, see Tutorial: Writing Transact-SQL Statements and the Transact-SQL Reference (Database Engine).
For other ways to connect to SQL Server on Linux, see the Develop and Manage areas.