SQL Server query to find all current database names

Sql Server

Sql Server Problem Overview


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

Sql Server Solutions


Solution 1 - Sql Server

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

Select * from Sys.Databases

Solution 2 - Sql Server

SELECT name  
FROM sys.databases

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

Solution 3 - Sql Server

Another to add to the mix:

EXEC sp_databases

Solution 4 - Sql Server

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

Solution 5 - Sql Server

You can also use these ways:

EXEC sp_helpdb

and:

SELECT name FROM sys.sysdatabases

Recommended Read:

Don't forget to have a look at sysdatabases VS sys.sysdatabases

A similar thread.

Solution 6 - Sql Server

This forum suggests also:

> SELECT CATALOG_NAME AS DataBaseName FROM INFORMATION_SCHEMA.SCHEMATA

Solution 7 - Sql Server

For people where "sys.databases" does not work, You can use this aswell;

SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.COLUMNS

Solution 8 - Sql Server

SELECT datname FROM pg_database WHERE datistemplate = false

#for postgres

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob JonesView Question on Stackoverflow
Solution 1 - Sql ServermmxView Answer on Stackoverflow
Solution 2 - Sql ServerRemus RusanuView Answer on Stackoverflow
Solution 3 - Sql ServerbeachView Answer on Stackoverflow
Solution 4 - Sql ServerbeachView Answer on Stackoverflow
Solution 5 - Sql ServerNeverHopelessView Answer on Stackoverflow
Solution 6 - Sql ServermatemaciekView Answer on Stackoverflow
Solution 7 - Sql ServerCeesieGView Answer on Stackoverflow
Solution 8 - Sql ServerChiranjeevi KandelView Answer on Stackoverflow