Vaccano
Vaccano

Reputation: 82341

Is there a variable like @@ServerName but for the database name?

I need to get the name of the database I am currently running against (to use in a URL column).

I know I can get the server name by using @@ServerName. But there is no @@DatabaseName.

How can I get the database that is currently being run against?

Upvotes: 3

Views: 116

Answers (3)

Kapil
Kapil

Reputation: 504

You can use this function to get the database name which you are currently using

select DB_NAME()

Upvotes: 0

BBlake
BBlake

Reputation: 2398

You can use DB_NAME() to get the database name.

SELECT DB_NAME() AS DataBaseName

Upvotes: 1

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171411

Try using this:

select DB_Name()

An example where the database changes:

use master
select DB_Name()
use model
select DB_Name()

Upvotes: 5

Related Questions