sql

sqlcmd command to connect to a SQL server

The sqlcmd command is used to connect to a SQL server. This command can be used to run queries, create databases, and perform other tasks. In this post, we will learn how to connect to a SQL server using sqlcmd.

sqlcmd -S host_url -U user_name -P password

-- Example to connect to an AWS RDS
sqlcmd -S rds-instance.aws-rds.com -U sa -P pass@123

In the above sqlcmd command:

Host: rds-instance.aws-rds.com

user: sa

password: pass@123

If the sqlcmd command is successful and the connection is made with the server then you will see the output as below.

[email protected]: sqlcmd -S host_url -U user_name -P password
1>

Get all Databases on SQL Server

After making a connection with the SQL Server you can list all the databases by executing the below query in sqlcmd.

1>SELECT name, database_id, create_date
2>FROM sys.databases;
3>GO
Was this helpful?