MS SQL Server List of Databases

Is there a way to obtain a list of database names on a given server (instance) for MS SQL Server? I am using MSSQLServerDatabase on Windows-only for this project but am open to using something else if required just to obtain the list of database names.

select name from sys.databases

Thanks Tim. I just logged in to post that I found it by googling (should have tried that first, huh?) but you beat me to the punch! Thanks. :slight_smile:

If all you want is a list of DB names an alternative is the system stored procedure sp_databases - as in

EXEC sp_databases

Though Tim’s suggestion gives you a LOT more information.

Couple of really useful undocumented system stored procedures for MSSQL are MSforeachdb and MSforeachtable which execute a SQL statement for every db/table - as in

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'

returns the space allocated for each database on a server - [?] is a placeholder the DB engine fills with every database name

EXECUTE sp_MSforeachtable 'EXECUTE sp_spaceused [?];';

returns the space allocated for each table in a database

Obviously - these are undocumented and may not be about in future versions…

(Not sure why I felt a need to add that bearing in mind the original question but there you go!!!)

Thank you, Patrick. Very informative.