Get list of databases from SQL Server

Viewed 994762

How can I get the list of available databases on a SQL Server instance? I'm planning to make a list of them in a combo box in VB.NET.

14 Answers

Execute:

SELECT name FROM master.sys.databases

This the preferred approach now, rather than dbo.sysdatabases, which has been deprecated for some time.


Execute this query:

SELECT name FROM master.dbo.sysdatabases

or if you prefer

EXEC sp_databases

To exclude system databases:

SELECT [name]
FROM master.dbo.sysdatabases
WHERE dbid > 6

Edited : 2:36 PM 2/5/2013

Updated with accurate database_id, It should be greater than 4, to skip listing system databases which are having database id between 1 and 4.

SELECT * 
FROM sys.databases d
WHERE d.database_id > 4
SELECT [name] 
FROM master.dbo.sysdatabases 
WHERE dbid > 4 

Works on our SQL Server 2008

If you are looking for a command to list databases in MYSQL, then just use the below command. After login to sql server,

show databases;

Related