I need a SQL query to find the names of existing databases.
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.
This forum suggests also:
SELECT CATALOG_NAME AS DataBaseName FROM INFORMATION_SCHEMA.SCHEMATA