Tutorial :SQL Server query to find all current database names



Question:

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


Solution:1

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

Select * from Sys.Databases  


Solution:2

SELECT name    FROM sys.databases  

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


Solution:3

Another to add to the mix:

EXEC sp_databases  


Solution:4

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

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

This forum suggests also:

SELECT CATALOG_NAME AS DataBaseName  FROM INFORMATION_SCHEMA.SCHEMATA  


Solution:7

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

SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.COLUMNS  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »