Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlServer 08: Query to list all databases in an instance?

How do I list all the databases for a given sql server 08 instance using sqlcmd?

like image 975
Dane O'Connor Avatar asked Jan 18 '10 17:01

Dane O'Connor


People also ask

What is the query to list all the databases?

1. System databases: The command to see system databases are : SELECT name, database_id, create_date FROM sys.

How can I see all databases on a database server?

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.

How do you run the same query on all the databases on an instance?

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.


2 Answers

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.
like image 105
Heinzi Avatar answered Oct 03 '22 00:10

Heinzi


To elaborate with more detail for the sqlcmd newbie:

C:\> sqlcmd -S <the_server_name> 1> select name from sys.databases 2> go 
like image 33
Shaun Luttin Avatar answered Oct 02 '22 22:10

Shaun Luttin