gjutras
gjutras

Reputation: 764

How do i find out in sql what db name I'm connected to

We have a change control environment where the developers give scripts to change control people to run. we have dev,qa, & production environments.

I want to conditionalize a couple segments to do some different things depending on what database the change control person is running my script.

If @dbname='dev'
then
begin
 --do some dev stuff
end
If @dbname='QA'
then
begin
 --do some qa stuff
end
If @dbname='Prod'
then
begin
 --do some production stuff
end

How do I get at what the current connected database is and fill @dbname?

Upvotes: 10

Views: 15136

Answers (3)

Hans Olsson
Hans Olsson

Reputation: 55049

I think it's just like:

SELECT DB_NAME() AS DBName

Upvotes: 17

John Hartsock
John Hartsock

Reputation: 86892

Use the system function db_name()

Select db_Name()

Upvotes: 7

Henric
Henric

Reputation: 1410

SELECT db_name() should do the trick.

Upvotes: 8

Related Questions