Can anyone help with this problem? I am referring to a example from the internet for executing T-SQL statements in parallel.
I want to be able to execute the same T-SQL on the same instance at once for proof of concept work on locking. In order to do this, I have tweaked the script so that I can execute any number of iterations by changing the
while ($counter -le 5)
Here is the full script. Basically the primary statement can be whatever T-SQL you want and this will populate $sqlcmds to have that statement passed through as many iterations as you wish.
Import-Module sqlps -DisableNameChecking;
Set-Location c:
# create a workflow to run multiple sql in parallel
workflow Run-PSQL #PSQL means Parallel SQL {
Param(
[Parameter(Mandatory=$true)]
[string]$ServerInstance,
[Parameter(Mandatory=$false)]
[string]$Database,
[Parameter(Mandatory=$true)]
[string[]]$Query # a string array to hold t-sqls
)
foreach -parallel ($q in $query) {
Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $q -QueryTimeout 60000;
}
} # Run-PSQL
# prepare a bunch of sql commands in a string arrary
#####new bit to make it dynamic sql multiple times
[string[]]$sqlcmds
$sqlcmds = ""
$counter = 0
do {
"Starting Loop $Counter"
$PrimaryStatement = '"SELECT TOP 1 * FROM sys.objects"'
if ($counter -eq 5) {
$sqlcmds = $sqlcmds + "$PrimaryStatement"
Write-Host "this is what sqlcmds is $sqlcmds loop 5"
} else {
$sqlcmds = $sqlcmds + "$PrimaryStatement,``"
Write-Host "this is what sqlcmds is now $sqlcmds"
}
$counter++
} while ($counter -le 5)
# now we can run the workflow and measure its execution duration
$dt_start = Get-Date; #start time
Run-PSQL -Server &&&&&&& -Database master -Query $sqlcmds;
$dt_end = Get-Date; #end time
$dt_end - $dt_start; # find execution duration
When this is executed, I get this message:
Run-PSQL : Cannot bind argument to parameter 'Query' because it is an empty string.
$sqlcmdshave a value? Does elastic syntax apply to workflows too? In this case, will-Serverbind to$ServerInstance, despite being only a prefix?