Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Get the name of the current database

Author  Topic 

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2005-03-07 : 04:06:01
A quick one (can't find it).

How to get the name of the database you are in?

I know the servername (SELECT @@SERVERNAME), But how do I get the database I'm currently working in?

It seems soooo basic, but can't find it.

Henri
~~~~
Anyone who can walk to the welfare office can walk to work.
- Al Capone

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-03-07 : 04:37:33

select distinct left(name,charindex('_',name)-1) from sysfiles


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2005-03-07 : 06:29:31
Thx, this works in a lot of the cases. Unfortunately we have a convention that makes the filesnames different than the database name. Do you have other suggestions?

Henri
~~~~
Anyone who can walk to the welfare office can walk to work.
- Al Capone
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-07 : 08:49:00
select db_name()

Be One with the Optimizer
TG
Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2005-03-07 : 08:52:25
Thx TG!

How does one get such knowledge?

Henri
~~~~
Anyone who can walk to the welfare office can walk to work.
- Al Capone
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-07 : 09:15:47
I've done a lot of BOLing ;~)

(reading Books-On-Line, that is)

Be One with the Optimizer
TG
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-03-07 : 09:22:05
quote:
Originally posted by henrikop

How does one get such knowledge?



SQLTeam!

And BOL, and Google, and....



Brett

8-)
Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2005-03-07 : 09:29:16
I love SQLTeam, and it helps me a lot, and my question seemed silly. But searching the SQLTeam forums usualy gives me time outs. And I did search on Google and such, but somehow I used the wrong keywords. "Retrieve Databasename SQL" did give a lot of results so it was hard to find the right information.

Posting in SQLTeam always works! But sometimes I don't want to look silly and aks questions that are asked a 1000 times. Getting to my goals weights more than shame, so I did post my question.



Henri
~~~~
Anyone who can walk to the welfare office can walk to work.
- Al Capone
Go to Top of Page
   

- Advertisement -