Avoid Blunders with SQL Query execution – Check where you are running the script

For a second, I thought I am running database restore script on my Production environment! Boom… &^%#*)!
Just a check confirms I am running it on Dev environment as expected, I am relieved. But it made me to think how to avoid running such scripts in production, it is a simple trick!

-- Validate on which server the script is executed. If the Server is not DEV_ServerName, JUST STOP! 
IF(@@SERVERNAME <> 'DEV_ServerName')
	raiserror('Nooooo... Not the correct Server - Check where you are running the script', 20, -1) with log
-- Actual SQL Query

Added above script at the beginning of the Database restore script. It fails if you run it on a different Server. Now, even If I run the script on a different server it will stop 

I have to restore Production database into Dev environment on demand. The request is very frequent, I have created a script to complete the work (Taking backup of all users, Restoring the latest database from Prod, Updating Personnel information like mails, Restoring the permission from the backup) in just one execution from my local machine. Imagine the risk of running it in prod environment  and make sure to include validation!

It is best recommend to include the validation if we are expecting to run the script on specific Server!

Related Blog (s):

This entry was posted in Productivity, Script, SQL, SQL Server and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s