Run T-SQL query parallel using PowerShell

T-SQL query can be run parallel using combination of PowerShell Workflows and Invoke-Sqlcmd cmdlet as show in following code:

param
(
    [string] $query = $(throw "Query is required"),
    [string] $database = $(throw "Database name is required"),
    [int] $parallelCount = 10
)

workflow Invoke-SqlcmdParallel
{
    param
    (
        $query,
        $parallelCount,
        $database
    )

    $numbers = [System.Linq.Enumerable]::Range(1, $parallelCount)

    foreach -parallel ($number in $numbers)
    {
        Invoke-Sqlcmd -Database $database -Query $query
    }
}

Invoke-SqlcmdParallel -query $query -parallelCount $parallelCount -database $database

Using this script you can easily simulate concurrency.

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.


Udemy course: Improve your productivity with PowerShell