views:

28

answers:

3

Hi guys,

How can I get all the database names in a sql server instance using tsql?

Great thanks.

+3  A: 
SELECT * FROM sys.databases
Thomas
A: 

this should work on pretty much any version of sql server

USE master;
SELECT NAME FROM sysdatabases;

[edit : it could be SELECT NAME FROM sys.databases too, microsoft's website says both and i'm not on my windows box to test, sorry!]

you could also use (sql 2005 only)

USE master;
EXEC sp_databases;
oedo
+2  A: 

----SQL SERVER 2005 System Procedures

EXEC sp_databases
EXEC sp_helpdb

----SQL 2000 Method still works in SQL Server 2005

SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases

----SQL SERVER Un-Documented Procedure

EXEC sp_msForEachDB 'PRINT ''?'''

to know more about database : http://blog.sqlauthority.com/2007/05/12/sql-server-2005-list-all-the-database/

Pranay Rana