I need an SQL query to search for the names of existing databases.
Here is a query to display all databases in one Sql engine
Select * from Sys.Databases
SELECT name FROM sys.databases
You will see only those databases for which you have permission.
Another addition to the mix:
EXEC sp_databases
I do not recommend this method ... but if you want to go stupid and weird:
EXEC sp_MSForEachDB 'SELECT ''?'' AS DatabaseName'
or
EXEC sp_MSForEachDB 'Print ''?'''
You can also use the following methods:
EXEC sp_helpdb
and
SELECT name FROM sys.sysdatabases
Recommended reading:
Remember to take a look at sysdatabases VS sys.sysdatabases
A similar stream .
This forum also offers:
SELECT CATALOG_NAME AS DataBaseName FROM INFORMATION_SCHEMA.SCHEMATA
For people where " sys.databases " does not work, you can also use this;
SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.COLUMNS