SQL SERVER – Get Current Database Name

Yesterday while I was writing script for SQL SERVER – 2005 – Find Unused Indexes of Current Database . I realized that I needed SELECT statement where I get the name of the current Database. It was very simple script.

SELECT DB_NAME() AS DataBaseName

It will give you the name the database you are running using while running the query.

SQL SERVER - Get Current Database Name dbname

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts
Previous Post
SQL SERVER – 2005 – Find Unused Indexes of Current Database
Next Post
SQL SERVER – Introduction to Three Important Performance Counters

Related Posts

113 Comments. Leave new

  • Thank Q for ur article….. it’s very useful….

    Reply
  • Thank you.

    Reply
  • Timmus Agersea
    July 20, 2008 1:22 am

    Thank you for sharing!

    Reply
  • Thank you so much…

    Reply
  • Thanl u very useful for us

    Reply
  • I look all the time for sql server info, and always see your smiling picture, with a clear answer, so thanks!

    Reply
  • Perfect, just what I was looking for, and faster to find your post than searching BOL!

    Reply
  • Thanks a lot!!!!!!!!

    Reply
  • I needed to run a report that spanned multiple servers, as well as most of their databases. This works very well combined with the undocumented sp_MSforeachDB.

    sp_MSforeachDB ‘use [?]; select user_code, u_name, superuser, lastlogin, groups, db_name() as database_name from ?..OUSR;’

    Using the above allows me to search every database on the server for information in the OUSR table. If the table is not found it will of course throw an error message. But this works great for running a quick report.

    Reply
  • santhosh v kumar
    December 19, 2008 5:48 am

    It’s really awesome… Can any one help me how to find path of database stored

    Reply
  • @Santhost v Kumar

    Run this command in the database whose file location you want to know.

    select filename from sysfiles

    to get more details

    select * from sysfiles

    It will display all the files (.mdf and .ldf ), their physical names ( existing on Operating System) and logical names ( used inside sql server ) and location of these files on Operating system.

    You can also check this using SSMS.

    > Open SQL Server Management Studio.
    >Open Object Explorer ( click View located on top ,select Object Explorer )
    > Expand Databases
    > Right click database name , click properties
    > A new Dialogue box will open, Select Files from left side List names. on the right side, slide bar to the right to see more information, you can see Location of database files on Operating System ( this is under Path column ).

    Regards
    IM.

    Reply
  • Sankara Narayanan
    December 24, 2008 4:39 pm

    hi,

    how can i get value from one database to another database?

    thanks & regards
    Sankara Narayanan

    Reply
  • Hi,

    If that database is on the same server, then you can use

    DBNAME.dbo.TableName to access the value from the table

    Thanks,

    Tejas

    Reply
  • Hi….
    I wan to get All the DataBase Names in server
    Can you give me a Query………
    And ther above query is very usefull……..
    Thanking u
    Regards
    Pavan

    Reply
  • how can i get db name from where the record retrieved
    for ur info: i use union syntax
    for example:
    DBName CustNo CustName
    db1 cust1 custA
    db1 cust2 custB
    db2 cust1 custA
    db3 cust1 custA

    Reply
  • GREAT! You help me another time :)

    Reply
  • Thanks,
    it was helpfull..
    can you help me how can i list all the table names of the database..
    Padma

    Reply
  • I am running a stored procedure from the master database in a database named “FOO”. Executing DB_NAME() returns “master”. How do you return “FOO”?

    Reply
  • Hi,

    Thanks a lot. The DB_NAME() command used in my coding. Thanks once again.

    Reply
  • I want to supply db name from ouitside.how its posible

    like
    declare @DBNAME sysname ,@stm varchar(100)
    Set @DBNAME =’database name ‘
    use database name

    Reply

Leave a Reply