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

  • I am trying to design a UI so internal users can backup their DB easily and the ability to find out the DB name is very useful. Thanks!

    Reply
  • Thanks! This is exactly what I’m after.

    Reply
  • how to list all database name and table names(describe table) in a server.
    ie,
    ————————
    db_name: DB1
    table_name:table1
    attribute_name data type width
    a varchar 50
    b varchar 50
    …………………………………………….
    table_name:table2
    attribute_name data type width
    c varchar 50
    d varchar 50

    ———————————
    db_name: DB 2
    table_name:table 4
    attribute_name data type width
    a varchar 50
    b varchar 50

    Reply
  • thanks sir that is interview question

    Reply
  • Grasias man me a cido util…! XD

    Reply
  • thank thank thank u

    Reply
  • hi,

    it’s good one.

    but, i want to know “how many databases in that particular server”?
    how to know?
    please post it to [email removed]

    Reply
  • Great post. While this script was really simple, but it saved me from hours of trial and error operations to get the name of the current database.

    I was planning to use sys.databases catalog view for that.

    Thanks.

    Reply
  • Thank you.

    Reply
  • Amazing!!!! I could have read this exact same thing in the SQL Help but this is so much better!

    Reply
  • hi…

    how can i get the database name of a particular column.

    thanks in advance.

    Reply
  • Thanks
    Help me…………………

    Reply
  • Hi …
    how can i copy databases from sql server 2005 to CD or flash memory..

    THx ..

    Reply
  • yes its very useful to find data base name
    __thanks a lot

    Reply
  • please help me…

    In my form -button1 -name: conn
    button2: name: orders
    Insert data into the table with the help of dataset, datagridview and command builder class
    * command builder class is used to create DML syntaxes
    * command builder class works with the help of data adapter class

    i am write code in below way but cannot execute this code and after build my program error msg is displayed:

    error msg like this: Error 1 Non-invocable member ‘System.Data.DataSet.Tables’ cannot be used like a method. D:\sudhakar\webapp\database\database\Form1.cs 51 43 database

    private void conn_Click(object sender, EventArgs e)
    {
    //SqlConnection con = new SqlConnection();
    con = new SqlConnection(“server=COMPUTER-9C3215; uid=sa; pwd=btp@123; databse=sudha”);
    con.Open();
    MessageBox.Show(“connection is opend”);
    // string q=””;
    SqlDataAdapter da = new SqlDataAdapter(“select * from test”, con);
    da.Fill(ds, “test”);
    //DataSet1 ds = new DataSet1();
    DataSet ds = new DataSet();
    MessageBox.Show(“data is filled:” + con.State.ToString());
    }

    private void button4_Click(object sender, EventArgs e)
    {

    dataGridView1.DataSource = ds.Tables(“t”);
    }

    Note: My error is :Error 1 Non-invocable member ‘System.Data.DataSet.Tables’ cannot be used like a method. D:\sudhakar\webapp\database\database\Form1.cs 51 43 database

    after build: keyword is not supported

    Reply
  • Hi, I have my database on a shared database server. I have been not using that database for quite some time now and now I need to retreive some old data. All I remember are few table names in the database. Is there a way, to find my database name on a shared database server using the table names?

    Reply
  • Thanks a lot. You have been a great help .

    Reply
  • Aliasger Bhanpurawala
    May 25, 2012 1:24 pm

    THANKS SIR,,,

    Reply
  • thanks…………………….

    Reply
  • Thank u sir….

    Reply

Leave a Reply