← 返回命令列表

Linux command

sqlcmd 命令

趣味

复制后可按需替换文件名、目录或参数。

常用示例

Connect to SQL Server

sqlcmd -S [localhost] -U [sa] -P "[password]"

Connect to a remote server

sqlcmd -S [server.example.com] -U [username] -P "[password]"

Execute a query

sqlcmd -S [server] -U [user] -P "[password]" -Q "SELECT @@VERSION"

Execute a SQL script file

sqlcmd -S [server] -U [user] -P "[password]" -i [script.sql]

Output results to a file

sqlcmd -S [server] -U [user] -P "[password]" -Q "[query]" -o [output.txt]

Connect with Windows Authentication

sqlcmd -S [server] -E

Connect with encryption optional

sqlcmd -S [server] -U [user] -P "[password]" -No

说明

sqlcmd is Microsoft's command-line utility for SQL Server and Azure SQL. It allows executing Transact-SQL statements, stored procedures, and SQL scripts interactively or in batch mode. Two implementations exist: the newer Go-based version (go-sqlcmd) and the traditional ODBC-based version. The utility supports various authentication methods including SQL Server authentication, Windows/Kerberos authentication, and Azure Active Directory. Results can be output to the terminal, files, or piped to other commands. In interactive mode, commands like :quit exit the session, GO executes the statement batch, and :r includes a script file. The prompt shows the current line number.

参数

-S _server_
SQL Server instance to connect to. Format: protocol:server\instance,port.
-U _login_
User login name.
-P _password_
Password for the login.
-E
Use Windows Authentication (trusted connection).
-d _database_
Initial database to use.
-Q _query_
Execute query and exit.
-q _query_
Execute query and enter interactive mode.
-i _file_
Input file containing SQL statements.
-o _file_
Output file for results.
-N _mode_
Encryption mode: o (optional), m (mandatory, default in SQL Server 2025), s (strict/TDS 8.0).
-No
Encryption optional (shorthand).
-C
Trust server certificate.
-t _timeout_
Query timeout in seconds.
-l _timeout_
Login timeout in seconds.
-h _headers_
Rows between column headers (-1 to disable).
-s _separator_
Column separator character.
-w _width_
Screen width for output.
-A _size_
Request a packet of a different size. Must be between 512 and 32767.
-W
Remove trailing spaces from a column.
-?
Display help for ODBC sqlcmd flags.
--help
Display help for go-sqlcmd subcommands.

FAQ

What is the sqlcmd command used for?

sqlcmd is Microsoft's command-line utility for SQL Server and Azure SQL. It allows executing Transact-SQL statements, stored procedures, and SQL scripts interactively or in batch mode. Two implementations exist: the newer Go-based version (go-sqlcmd) and the traditional ODBC-based version. The utility supports various authentication methods including SQL Server authentication, Windows/Kerberos authentication, and Azure Active Directory. Results can be output to the terminal, files, or piped to other commands. In interactive mode, commands like :quit exit the session, GO executes the statement batch, and :r includes a script file. The prompt shows the current line number.

How do I run a basic sqlcmd example?

Run `sqlcmd -S [localhost] -U [sa] -P "[password]"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -S _server_ do in sqlcmd?

SQL Server instance to connect to. Format: protocol:server\instance,port.