How do I list all the databases for a given sql server 08 instance using sqlcmd?
1. System databases: The command to see system databases are : SELECT name, database_id, create_date FROM sys.
Open the Command Prompt and navigate to the bin folder of your MySQL Server installation directory. Then connect to the server using the mysql -u root -p command. Enter the password and execute the SHOW DATABASES; command we have discussed above.
There is a handy undocumented stored procedure that allows you to do this without needing to set up a cursor against your sysdatabases table in the master database. This can be done by using sp_MSforeachdb to run the same command in all databases.
sqlcmd -E -S SERVER\INSTANCE -Q "sp_databases"
Notes:
-E
: Use a trusted connection ("Windows authentication"). Replace by -U username -P password
for SQL Server authentication.-S SERVER\INSTANCE
: The instance of SQL Server to which to connect. If you don't know the name of your instance, you can use sqlcmd -L
to get a list.-Q
: The query to execute. The uppercase Q
causes sqlcmd to exit after executing the query.To elaborate with more detail for the sqlcmd newbie:
C:\> sqlcmd -S <the_server_name> 1> select name from sys.databases 2> go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With