Cry How to...

Determine the name of the database you are connected to


To get the name of the current database:

select db_name(dbid) from master..sysprocesses where spid=@@SPID

alternately:

sp_who @@SPID

will give the name of the current database and other information about the current user.


These notes have been tested against SQL Server 7 and SQL Server 2000 and SQL Server 2005.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.