Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Database Name?

Status
Not open for further replies.

Savil

Programmer
Apr 28, 2003
333
EU
Hi
Does anybody know of a quick method to return the current database name?

Thanks Savil
 
Try:
Code:
SELECT NAME FROM sysfiles WHERE fileid = 1


Krickles | 1.6180

 
When I load a new db build onto the server the db is restored as tempMydb. Several tables are loaded with data from the old Mydb and then the old Mydb is deleted and the new one is renamed. I want to have a SP that can reside in both the old Mydb and the new tempMydb but i only want some actions performing as long as the current db name is tempMydb and not the renamed Mydb.

Confused? so am I
 
You want to dynamically call the db based on the db properties? As far as I know this can't be done directly. However, you can try using CASE statements...depending on how many actions you are performing this can get verbose. Sample:
Code:
SELECT fieldname1, fieldname2, ..., fieldnameN,
CASE WHEN (SELECT NAME FROM sysfiles WHERE fileid=1) = 'tempMydb'
THEN fieldnameX
ELSE [i]another action?[/i]
END
FROM tablename

Krickles | 1.6180

 
Krickles
I think your first solution will work,

as there will only be two db names tempMyDb and MyDb then I can use ltrim, rtrim, like, len etc. Harry Potter wizzardry to always return the correct name

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top