3

Given an instance of SQL server, what's the best way to enumerate the databases?

3 Answers 3

7

In SQL Server 2000+:

select * from sysdatabases

In SQL Server 2005+:

select * from sys.databases

The difference is subtle and barely worth mentioning for a one-liner like this. But depending on how much you're going to be accessing the system catalog, you may get some use out of this article:

Querying the SQL Server System Catalog

You can also execute sp_helpdb without an argument to get basic information about all databases. (Pass in a database name as an argument to get more detailed information about that database).

Sign up to request clarification or add additional context in comments.

2 Comments

Note that there is no sys schema in SQL 2000, is just select * from sysdatabses
@Remus Thanks, guess it's been too long since I needed 2000-compatible syntax!
4

Try this:

SELECT [name] FROM sys.sysdatabases

You could also get this from exec sp_databases;

These commands will work from SQL 2000+.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.