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

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;

SELECT * FROM sys.databases

----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/