Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql-server

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?

like image 916
gjutras Avatar asked May 25 '10 14:05

gjutras


3 Answers

I think it's just like:

SELECT DB_NAME() AS DBName
like image 181
Hans Olsson Avatar answered Nov 06 '22 23:11

Hans Olsson


SELECT db_name() should do the trick.

like image 27
Henric Avatar answered Nov 07 '22 00:11

Henric


Use the system function db_name()

Select db_Name()
like image 6
John Hartsock Avatar answered Nov 07 '22 00:11

John Hartsock