How can I retrieve my current database name?

  • I am sure this is simple, but I cant find the answer anywhere I have looked.  So, I am either not looking for the right thing, or ????  Anyway, in a sproc, I would like to retrieve the name of the current database to which I am attached (i.e. the value I would place on the USE command).

    SET @MyDBName = ???????

  • SELECT DB_NAME()



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • DOH!

    Thank you 🙂

  • Be cautious of "niladic" functions (like db_name(), user_name(), object_name(), etc.) if they  being used in a stored procedures. They are "context" specific. In other words some of these functions will return different values dependent on how they are executed. Some examples:

     

    use master

    go

    exec sp_whatever

     

     

    use another_db

    go

    exec master..sp_whatever

     

    In your case db_name() should work the same no matter what the execution "context".

    RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."

Viewing 4 posts - 1 through 3 (of 3 total)

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