Obtaining the database name?

  • I'm having a major Monday brainfart today.

    We've got a report that pulls data from one of two different databases (that are on the same server).  My co-worker and I know which database we're in based on where we logged into - from a techie standpoint.  However, our ordinary users wouldn't know for sure where they're logged in.  I want to put the database name on the report, but I haven't been able to find anything in BOL to steer me the right direction.

    Is there an @@ variable or some way for me to pull the database name in T-SQL?

    Thanks in advance!

  • This should work: select

    DB_NAME(DB_ID())

  • That did it!  Thank you very much!

  • select db_name() will do

  • You can also check for existing of database using following sql statement

    SELECT NAME FROM SYSDATABASES WHERE NAME='databasename'

     

  • This last one wouldn't have helped me out, as I wouldn't know which databasename to send - I was trying to figure out which database I was in.

    As it is, if I knew the name, why would I want to select it off of sysdatabases when I already knew it?

    Thanks anyhow.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply