How To Connect To SQL Server From cmd command line

In some cases, SSMS (SQL Server Management Studio) is not installed on the server and you may need to connect to SQL Server from the cmd command line immediately.

In this case, you should run cmd by right-clicking the command line and clicking run as administrator to connect to the sql server. Then you can connect to SQL Server with the help of the following command.

.\ It represents your current server. You can write hostname\ instead instead of this.
TESTINSTACE2 Name of Named instance
-E It means Trusted Connection. Do not change this.
-S It means Server. Do not change this.

If you want to connect to default instance instead of named instance, you can connect by typing sqlcmd only.

Queries that you run after connecting from the cmd command line must be specific. You should not run with “Select * From...“. If you run this way, it will be difficult to read the incoming query result.

For example, you should only select the column you need, such as “Select name From sys.databases“.

You can use the following script to view the current summary of queries (if there is a lock or not).