0

The following query returns the name of the current database:

USE [DB1]
SELECT DB_NAME() AS Current_DB
FROM [dbo].[TblName]

Result:

Current_DB
----------
DB1

I'm seeing very strange results when using DB_NAME() with queries that SELECT FROM a table in [DBName].[SchemaName].[TblName] notation:

T-SQL

USE [DB1]
SELECT DB_NAME() AS Current_DB
FROM [DB1].[dbo].TblName
UNION
SELECT DB_NAME() AS Current_DB
FROM [DB2].[dbo].TblName
UNION
SELECT DB_NAME() AS Current_DB
FROM [DB3].[dbo].TblName
UNION
SELECT DB_NAME() AS Current_DB
FROM [DB4].[dbo].TblName

Result:

Current_DB
----------
DB1

T-SQL

USE [DB1]
SELECT DB_NAME() AS Current_DB
FROM [DB1].[dbo].TblName
UNION ALL
SELECT DB_NAME() AS Current_DB
FROM [DB2].[dbo].TblName
UNION ALL 
SELECT DB_NAME() AS Current_DB
FROM [DB3].[dbo].TblName
UNION ALL
SELECT DB_NAME() AS Current_DB
FROM [DB4].[dbo].TblName

Result:

Current_DB
----------
DB1
DB1
DB1
...  (632,788 rows of DB1 !!!)

Even if I omit the USE [DB1] from T-SQL query, I get the same results - the database selected in the Query Target dropdown menu of SSMS is the one appearing in the results.

How can I get the correct DB_NAME() across multiple queries using [DBName].[SchemaName].[TblName] format?

3
  • 1
    As you said returns the name of the current database. Not the database of the table(s) in the FROM clause. Commented Feb 26, 2019 at 12:04
  • what you are trying to achieve? you already know the db name when you use [DB1].[dbo].TraceContactSource Commented Feb 26, 2019 at 12:08
  • You can't. The only way to get DB_NAME() to return the name you want is to do use [dbname] first. And if you can do that, you already have the name... Commented Feb 26, 2019 at 12:09

3 Answers 3

4

DB_NAME() without parameter shows the database name of the currently connected session, not the underlying database of the object you are querying.

From the docs:

A. Returning the current database name. This example returns the name of the current database.

SELECT DB_NAME() AS [Current Database];

Otherwise what would you expect if you do a join between databases and call DB_NAME()?

SELECT
    DB_NAME() -- ??
FROM
    Database1.dbo.Table1 AS T
    CROSS JOIN Database2.dbo.Table2 AS N

You can switch connected databases with the USE <DatabaseName> statement.

Sign up to request clarification or add additional context in comments.

Comments

1

The DB_NAME() function returns the name of the current database if the database_id argument is not specified.

current database does not mean the database where the table you are querying lives, but the database you are currently connected to.

The difference between the union and the union all queries is that union returns distinct values - and since you are selecting the same value in every query, a single value is returned.

3 Comments

Thanks. Any idea why the UNION ALL is returning 632,788 even though only 4 queries are UNION ALL'd ?
Yes - there are a total of 632,788 rows in all the tables combined. If you where to do (select count(*) from table1) + (select count(*) from table2) and so on that's the number you would get.
Thanks, that makes sense! :)
0

DB_NAME() returns the name of the database you are currently connected to. A query can reference multiple databases in the FROM, so what would expect for a query like?

SELECT DB_NAME()
FROM DB1.dbo.YourTable YT
     JOIN DB2.dbo.OtherTable OT ON OT.ID = YT.ID;

For your query, you need to specify the name of the database in your query:

USE [DB1]
SELECT N'DB1' AS Current_DB
FROM [DB1].[dbo].TraceContactSource
UNION ALL
SELECT N'DB2' AS Current_DB
FROM [DB2].[dbo].TraceContactSource
UNION ALL 
SELECT N'DB3' AS Current_DB
FROM [DB3].[dbo].TraceContactSource
UNION ALL
SELECT N'DB4' AS Current_DB
FROM [DB4].[dbo].TraceContactSource

As per the comments, however, if you're actually after a list of (non-system) databases, perhaps this achieves your goal:

SELECT [name] AS Current_DB
FROM sys.databases
WHERE database_id > 4;

4 Comments

Unless adding records from the actual tables, the from clause is redundant - you can simply do SELECT N'DB1' As Current_Db UNION ALL SELECT N'DB2' UNION ALL SELECT N'DB3'...
Infact, SELECT [name] AS Current_Db FROM sys.databases WHERE database_id > 4; might be better, depending on their goal, @ZoharPeled, depending on their goal.
I'm looking for a way to do this without hard-coding the DB name in the query - I am using sp_executesql and running this code across 100+ db's to create a comparison report. I have found a way to do this now... thanks anyway :)
Sounds like what we had here was an XY problem, @Adam .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.