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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing for database name 1

Status
Not open for further replies.

rhysmeister

Programmer
May 29, 2003
54
GB
Is there a way to test for the database name at the top of a script so it will never run against the wrong one? I want something like...

Code:
IF SELECTEDDATABASE = <intended_database>
  Begin
    Do script actions
  End
Else
  Begin
    print "Error, this script is not permitted to run on this database!"
  End
 
Use db_name() to deturmin the current database. It will return nvarchar so you shouldn't need to convert the output to use it.
Code:
if db_name() = 'user_database'
BEGIN
  Do something
END
ELSE
BEGIN
  print 'Error'
END

Denny

--Anything is possible. All it takes is a little research. (Me)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top