I’m working on a way to automate Some environment setups in Azure, part of that includes setting up the Azure SQL Database.
I have a folder of 12 LARGE build scripts 10s of throusands of rows that our DBAs have made over the years, in order to setup the schema for our product you have to run them all in order.
I wrote a while loop to run through these scripts in order and even keep track of progress in a json file in the event something is interrupted as some of these take a while to run. the loop works fine when I use dummy queries.
unfortunately the issue seems to be that scripts that will run in SSMS, seem to have issues running programmatically.
I’ve tried a few different methods to no real success
by default I used SQLPS and “Invoke-Sqlcmd”, I set the timeout to 3600 seconds to give an hour just in case and the verbose output warns me about the script being too deeply nested, it’s really not more than 4-5 levels nested in some cases.
SSMS doesn’t complain it just chugs for a while and runs it, I also tried Invoke-Sqlcmd2 because I prefer using it since it’s much lighter and just leverages the existing .NET methods, but this gave the same error about nesting as above.
additionally I tried setting up my own SMO script using
Add-Type "Microsoft.SqlServer.Smo"
and
$db.ExecuteWithResults($Query)
and that just generates an exception, As best I can guess SSMS in some ways makes up for sloppy T-SQL and does some of the chunking the code up behind the scenes without telling you, the DBA is on contract so it’s not going to be an option to have them rewrite the scripts, so just trying to figure out if I should give up this dream and just get used to babysitting SSMS.
If nothing else I built a cool Script that fully automates resource setups using AzureRM Module
- $ds = $db.ExecuteWithResults(“$testscript”)
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : FailedOperationException