Are you on the correct server and the correct database with MSSQL
- It's very important to make sure that you are using the right server and the right database.
- Nothing worth that deleting data on a production server.
- Nothing worth than inserting test data in a production database.
Applies to:
- Microsoft SQL Server 2000
- Microsoft SQL Server 2005
Server and database name
- SELECT CAST(@@SERVERNAME AS VARCHAR(20)) AS 'Server',
- CAST(DB_NAME() AS VARCHAR(20)) AS 'Database'
- go
Server Database -------------------- -------------------- W2K3BASE master (1 row(s) affected)
- @@servername: returns a 128 long nvarchar string, name of the server. I truncated it with the cast to make it readable on the screen. You can change the server name with sp_addserver and by restarting the Microsoft SQL Server.
- db_name(): returns a 128 long nvarchar string, name of the server. I truncated it with the cast to make it readable on the screen.
Checking if you are on the correct server and the correct database
- IF @@SERVERNAME = 'W2K3BASE'
- BEGIN
- PRINT 'Server: W2K3BASE'
- END;
- IF DB_NAME() = 'master'
- BEGIN
- PRINT 'db: Master'
- END;
- IF DB_NAME() <> 'sql911'
- BEGIN
- PRINT 'Wrong database, currently in: ' + DB_NAME()
- END;
- go
Server: W2K3BASE db: Master Wrong database, currently in: master

