在Linux系统中使用sqlcmd命令连接与查询SQL Server

本文根据微软官方手册整理完成

https://docs.microsoft.com/zh-cn/sql/linux/sql-server-linux-connect-and-query-sqlcmd

 

This topic provides connection requirements and guidance for SQL Server vNext CTP 1.3 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 is a walk-through. In this walk-through, you will use sqlcmd to connect to SQL Server vNext on Linux. After successfully connecting, you will use Transact-SQL (T-SQL) queries to create and populate a small 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.

Install the SQL Server command-line tools

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:

Connection requirements

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.

Connect to SQL Server on Linux

In the following steps, connect to SQL Server vNext on Linux with sqlcmd.

Tip

On macOS, use sql-cli because sqlcmd and bcp are not available.

  1. On your Linux box, open a command terminal.

  2. 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.

    Copy

    bash

    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.

    Copy

    bash

    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.

Query SQL Server

After you connect to SQL Server you can run queries to return information or create database objects. If you are new to writing queries, see Writing Transact-SQL Statements. In the following steps, you will use sqlcmd to:

  1. Query SQL Server for a list of the databases.

  2. Use Transact SQL to create a database.

  3. Create and populate a table in the new database.

  4. Query the table.

To to complete each of these tasks, copy the Transact-SQL from the examples below into the sqlcmd session that you created in the previous step.

For example, this query returns the name of all of the databases.

Copy

sql

SELECT Name from sys.Databases;
GO

Create a database using the SQL Server default settings.

Copy

sql

CREATE DATABASE testdb;
GO

Use the database:

Copy

sql

USE testdb;
GO

Create a table in the current database:

Copy

sql

CREATE TABLE inventory (id INT, name NVARCHAR(50), quantity INT);
GO

Insert data into the new table:

Copy

sql

INSERT INTO inventory VALUES (1, 'banana', 150);
INSERT INTO inventory VALUES (2, 'orange', 154);
GO

Select from the table:

Copy

sql

SELECT * FROM inventory WHERE quantity > 152;
GO

To end your sqlcmd session, type QUIT.

Copy

sql

QUIT

In this walk-through you connected to SQL Server with sqlcmd, and created and populated a database. For more information on how to use sqlcmd.exe, see sqlcmd Utility.

Connect and query from Windows

It is important to note that SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server instance. So, you can follow the same steps in this topic running sqlcmd.exe from a remote Windows machine. Just verify that you use the target Linux machine name or IP address rather than localhost. For other connection requirements, see connection troubleshooting recommendations.

For other tools that run on Windows but connect to SQL Server on Linux, see:

Next Steps

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.

转载于:https://my.oschina.net/chipo/blog/841487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值