SQL Server query to find all current database names

Viewed 203662

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

8 Answers

Here is a query for showing all databases in one Sql engine

Select * from Sys.Databases
SELECT name  
FROM sys.databases

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

Another to add to the mix:

EXEC sp_databases

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 ''?'''

This forum suggests also:

SELECT CATALOG_NAME AS DataBaseName
FROM INFORMATION_SCHEMA.SCHEMATA
SELECT datname FROM pg_database WHERE datistemplate = false

#for postgres

Related