How to change database context dynamically
Posted on 2014-10-06
I have a script I need to run against ~300 databases. using stored procedures (sp_msforeachdb, exec) aren't viable because of batch separators in the form of GO statements throughout the script.
What's the best way to dynamically context and then run the script verbatim? The script is 1200+ lines, so I'm trying to avoid having to edit the script and dynamically assign fully qualified server/db names throughout.
Let me know if I didn't provide enough information.
basically I'm looking to do something like:
for each (@db in dbs)
run rest of script