internal/Test-SqlQueryComplete.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Function Test-SqlQueryComplete
{
    param (
        [Alias("SqlServer")]
        [object]$server,
        [string]$sql,
        [switch]$checkpid
    )
    
    if ($checkpid)
    {
        $sqlpid = " and session_id = $sqlpid"
    }
    
    $sqlpid = $server.ConnectionContext.ProcessID
    $sqlpid = " and session_id = $sqlpid"
    $sql = $sql.Replace("'", "''")
    $testsql = "select sqltext.text FROM sys.dm_exec_requests req CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext where text = '$sql' $sqlpid"
    
    if ($server.ConnectionContext.ExecuteScalar($testsql) -ne $null)
    {
        return $false
    }
    else
    {
        return $true
    }
}