Add option to stop processing if the database in the USE statement doesn't exist
If you run a script with option to use the "USE database" clause and the database doesn't exist the script will go ahead and attempt to run the script in the current database. This is usually "master" if you are in a new query window and if you only adding new items it will add them all to the master db.
-
Dan W commented
I have an implementation option to recommend. In keeping with the rest of the error handling, simply put the IF @@ERROR <> 0 SET NOEXEC ON after the USE [my_database] statement in the synchronization script, such as:
USE [my_database]
GO
IF @@ERROR <> 0 SET NOEXEC ON
GOThis doesn't protect against scripts from being run against the right database on the wrong server, but it at least it prevents scripts from being applied if the change to the expected target database fails.