March 15, 2018 by Kenneth Fisher

I’d never seen ORIGINAL_DB_NAME until recently and I thought it would be interesting to highlight it out, and in particular the difference between it and DB_NAME. I use DB_NAME and DB_ID fairly frequently in support queries (for example what database context is a query running from or what database are given DB files from). So starting with DB_NAME.


Simply enough, if you pass a database_id in, it returns the databases name, if you don’t it returns the current databases name.

USE tempdb;


You might think this is always the database the connection initially connected to but you would be wrong. In fact, it returns a blank unless you specify a database to connect to in your connection string or connection options, etc. USE has no effect on the output and neither does your default database. There is also no parameter so this is the only value it will show. Using SQLCMD to make this a bit easier to see:

Without specifying -d (connect to a specific database)

With specifying -d


Honestly, the only possible use I can see for ORIGINAL_DB_NAME is for some forms of logging/auditing. Good information to know, but I’ll still be using DB_NAME 99% of the time.

One thought on “DB_NAME() vs ORIGINAL_DB_NAME()

  1. […] Kenneth Fisher explains a couple of database name functions in SQL Server: […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,753 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: