**which displays the SQL Server version and related details.
**
sqlcmd -S localhost -U yourUsername -P yourPassword -d master -Q “SELECT @@VERSION;”
Command Breakdown
sqlcmd
– Command-line utility for working with SQL Server.-S localhost
– SQL Server name or address; here, it’s running locally.-U yourUsername
– Username for SQL Server Authentication.-P yourPassword
– User password.-d master
– Default database to connect to (master
here).-Q "SELECT @@VERSION;"
– Executes the query and exits afterward.Common sqlcmd
Options
Option / Command | Description |
---|---|
-S serverName\instanceName |
Specify the server and SQL Server instance. |
-U username |
Login name for SQL Server Authentication. |
-P password |
User password (prompted if omitted). |
-E |
Use Windows Authentication (no username/password needed). |
-d database |
Default database to connect to. |
-Q "query" |
Execute a query and exit. |
-i inputFile.sql |
Run commands from a SQL file. |
-o outputFile.txt |
Save output to a file. |
GO |
In interactive mode, executes the current SQL batch. |
:setvar name value |
Define a variable for use in the script. |
!! command |
Execute an operating system command (e.g., !! dir or !! ls ). |
:connect serverName |
Connect to a different server without exiting sqlcmd . |
:quit / EXIT |
Exit sqlcmd . |