tags:

views:

1116

answers:

5

I need a SQL query to find the names of existing databases.

+6  A: 
select name from master..sysdatabases
Mehrdad Afshari
+1  A: 

This forum suggests also:

SELECT CATALOG_NAME AS DataBaseName FROM INFORMATION_SCHEMA.SCHEMATA

matemaciek
This didn't work on my machine. MSDN says it is supposed to "contains one row for each database that has permissions for the current user." However the results were limited to only the current database. "SELECT * FROM sysdatabases" works better for me.
beach
+3  A: 
select name from sys.databases

You'll only see the databases you have permission to see.

Remus Rusanu
A: 

Another to add to the mix:

EXEC sp_databases
beach
A: 

I don't recommend this method... but if you want to go wacky and strange:

EXEC sp_MSForEachDB 'SELECT ''?'' AS DatabaseName'

or

EXEC sp_MSForEachDB 'Print ''?'''
beach