In SQL Server Management Studio, I repeatedly manage to run sql scripts against the master database rather the specific database that I intend. I open a SQL file and then click run, repeatedly forgetting to set the database first.
I know you can set a default for a server connection – but then I might end up running sql on the default database rather than my intended one if my intended one is not the default.
USE statements in the sql won’t work either – the scripts need to be run against multiple different test databases.
So is there anyway to flag a warning that the script is about to be run against the master database? Or possibly force the selection of a database first before a script is run?
One way is to connect with an account that has permissions only in the required databases. Not sysadmin which is what you are doing now.
This would be normal for developers
If you want to run as sysadmin, add this line to each script
This will kill the connection
Edit:
I like Quassnoi’s solution because it doesn’t require sysadmin rights.
But, you can’t give feedback or revert.
So, I’ve been playing with this. It checks most boxes
There is one issue that an SSMS USE (via the drop down) is ignored (as per Quassnoi’s notes), but it’s getting there. Any more ideas anyone?