文档章节

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

gugudu
 gugudu
发布于 2017/02/18 22:53
字数 791
阅读 290
收藏 0

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

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.

© 著作权归作者所有

gugudu
粉丝 9
博文 96
码字总数 45034
作品 0
天津
私信 提问
SQL Server on Linux 2017(初识 LINUX下的SQL-SERVER功能应用)

安装环境Centos7: 官方安装必要條件: 必须 RHEL 7.3 或 7.4 机至少 2 GB的内存 1、设置mssql_server的YUM官方源: curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsof...

jwenshan
2018/05/28
0
0
MSSQL-Server On Docker

安装先决条件: • 适用于支持的任一 Linux 分发版的 Docker 引擎 1.8 以上版本,或适用于 Mac/Windows 的 Docker。 有关详细信息,请参阅 Install Docker(安装 Docker)。 • 至少 2 GB 的...

jwenshan
2018/05/30
0
0
MSSQL(SQL Server) on Linux 简明部署与使用

标签 PostgreSQL , ms sql , SQL Server 背景 本文介绍MS SQL on Linux的简单部署,使用。 https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-serve......

德哥
04/14
0
0
在Linux上创建和运行SQL Server 2017代理作业

启用SQL Server代理 sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true sudo systemctl restart mssql-server 备注:从 SQL Server 2017 CU4 开始,SQL Server 代理是附带mssql ser......

UltraSQL
2018/07/23
0
0
在Linux上使用mssql-conf工具配置SQL Server 2017

在Linux上使用mssql-conf工具配置SQL Server 2017 mssql-conf是在Linux上安装SQL Server 2017后的一个配置脚本。你可以使用这个实用工具设置以下参数: 使用贴士: l 对于AlwaysOn可用性组和...

UltraSQL
2018/07/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

分布式协调服务zookeeper

ps.本文为《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之一 ZooKeeper ZooKeeper曾是Apache Hadoop的一个子项目,是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它...

ls_cherish
56分钟前
3
0
redis 学习2

网站 启动 服务端 启动redis 服务端 在redis 安装目录下 src 里面 ./redis-server & 可以指定 配置文件或者端口 客户端 在 redis 的安装目录里面的 src 里面 ./redis-cli 可以指定 指定 连接...

之渊
昨天
2
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888
昨天
4
0
hash slot(虚拟桶)

在分布式集群中,如何保证相同请求落到相同的机器上,并且后面的集群机器可以尽可能的均分请求,并且当扩容或down机的情况下能对原有集群影响最小。 round robin算法:是把数据mod后直接映射...

李朝强
昨天
4
0
Kafka 原理和实战

本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/bV8AhqAjQp4a_iXRfobkCQ 作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双语班)。先后从事过电子商务、开放平...

vivo互联网技术
昨天
24
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部