使用 sqlcmd 连接到 Synapse SQL 池Connect to Synapse SQL pool with sqlcmd

使用 [sqlcmd][sqlcmd] 命令行实用程序连接并查询 SQL 池。Use the [sqlcmd][sqlcmd] command-line utility to connect to and query a SQL pool.

1.连接1. Connect

若要开始使用 [sqlcmd][sqlcmd],请打开命令提示符并输入“sqlcmd” ,后跟 SQL 池的连接字符串。To get started with [sqlcmd][sqlcmd], open the command prompt and enter sqlcmd followed by the connection string for your SQL pool database. 连接字符串需要以下参数:The connection string requires the following parameters:

  • 服务器 (-S): 采用 <服务器名称>.database.chinacloudapi.cn 格式的服务器Server (-S): Server in the form <Server Name>.database.chinacloudapi.cn
  • 数据库 (-d): 数据库名称。Database (-d): Database name.
  • 启用带引号的标识符 (-I): 必须启用带引号的标识符才能连接到 SQL 池实例。Enable Quoted Identifiers (-I): Quoted identifiers must be enabled to connect to a SQL pool instance.

若要使用 SQL Server 身份验证,需添加用户名/密码参数:To use SQL Server Authentication, you need to add the username/password parameters:

  • 用户 (-U): 采用 <User> 格式的服务器用户User (-U): Server user in the form <User>
  • 密码 (-P): 与用户关联的密码。Password (-P): Password associated with the user.

例如,连接字符串可能如下所示:For example, your connection string might look like the following:

C:\>sqlcmd -S MySqlDw.database.chinacloudapi.cn -d Adventure_Works -U myuser -P myP@ssword -I

若要使用 Azure Active Directory 集成身份验证,需添加 Azure Active Directory 参数:To use Azure Active Directory Integrated authentication, you need to add the Azure Active Directory parameters:

  • Azure Active Directory 身份验证 (-G):使用 Azure Active Directory 进行身份验证 Azure Active Directory Authentication (-G): use Azure Active Directory for authentication

例如,连接字符串可能如下所示:For example, your connection string might look like the following:

C:\>sqlcmd -S MySqlDw.database.chinacloudapi.cn -d Adventure_Works -G -I

备注

启用 Azure Active Directory 身份验证 才能使用 Active Directory 进行身份验证。You need to enable Azure Active Directory Authentication to authenticate using Active Directory.

2.查询2. Query

连接后,可以对实例发出任何支持的 Transact-SQL 语句。After connection, you can issue any supported Transact-SQL statements against the instance. 在此示例中,查询以交互模式进行提交。In this example, queries are submitted in interactive mode.

C:\>sqlcmd -S MySqlDw.database.chinacloudapi.cn -d Adventure_Works -U myuser -P myP@ssword -I
1> SELECT name FROM sys.tables;
2> GO
3> QUIT

后续示例演示如何通过使用 -Q 选项或将 SQL 输送到 sqlcmd 从而在批处理模式下运行查询。These next examples show how you can run your queries in batch mode using the -Q option or piping your SQL to sqlcmd.

sqlcmd -S MySqlDw.database.chinacloudapi.cn -d Adventure_Works -U myuser -P myP@ssword -I -Q "SELECT name FROM sys.tables;"
"SELECT name FROM sys.tables;" | sqlcmd -S MySqlDw.database.chinacloudapi.cn -d Adventure_Works -U myuser -P myP@ssword -I > .\tables.out

后续步骤Next steps

有关 sqlcmd 中可用选项的详细信息,请参阅 sqlcmd 文档For more about details about the options available in sqlcmd, see sqlcmd documentation.