SQL 2014 Blocking Sessions Provider

Microsoft.SQLServer.2014.BlockingSPIDsProvider (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsMicrosoft.SQLServer.2014.SQLProbeAccount
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.TimedScript.PropertyBagProvider Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval (seconds)The recurring interval of time in seconds in which to run the workflow.
SyncTimestring$Config/SyncTime$Synchronization TimeThe synchronization time specified by using a 24-hour format. May be omitted.
WaitMinutesint$Config/IntervalSeconds$Wait Time (minutes)The minimum process execution duration before considering it for Blocked SPIDs analysis.
TimeoutSecondsint$Config/TimeoutSeconds$Timeout (seconds)Specifies the time the workflow is allowed to run before being closed and marked as failed.

Source Code:

<DataSourceModuleType ID="Microsoft.SQLServer.2014.BlockingSPIDsProvider" Accessibility="Internal" RunAs="SQL2014Core!Microsoft.SQLServer.2014.SQLProbeAccount">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ConnectionString" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SyncTime" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WaitMinutes" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="InstanceName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ComputerName" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
<OverrideableParameter ID="SyncTime" ParameterType="string" Selector="$Config/SyncTime$"/>
<OverrideableParameter ID="WaitMinutes" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.PropertyBagProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>GetSQL2014BlockingSPIDs.vbs</ScriptName>
<Arguments>"$Config/ConnectionString$" "$Config/WaitMinutes$" "$Config/InstanceName$" "$Config/ComputerName$" "$Target/Property[Type="SQL2014Core!Microsoft.SQLServer.2014.DBEngine"]/TcpPort$"</Arguments>
<ScriptBody><Script>'#Include File:Initialize.vbs

Option Explicit
SetLocale("en-us")

Const ManagementGroupName = "$Target/ManagementGroup/Name$"
Const ManagementGroupID = "$Target/ManagementGroup/Id$"
Const SQL_DEFAULT = "MSSQLSERVER"

Function Quit()
WScript.Quit()
End Function

Function IsValidObject(ByVal oObject)
IsValidObject = False

If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function

Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error

On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error GoTo 0

If oError.Number &lt;&gt; 0 Then ThrowScriptError "Unable to create automation object '" &amp; sProgramId &amp; "'", oError
End Function

Public Function GetSQLServiceName(sInstance)
If sInstance = SQL_DEFAULT Then
GetSQLServiceName = SQL_DEFAULT
Else
GetSQLServiceName = "MSSQL$" &amp; sInstance
End If
End Function

'The function returns service or "Unknown" state
'Input:
' server - compute name
' service - system service name
'Output:
' service state or "Unknown" state
Function GetServiceState( sTargetComputer, sServiceName)
On Error Resume Next

Dim sNamespace, sQuery, oWMI, objClasses, sState
sNamespace = "winmgmts://" &amp; sTargetComputer &amp; "/root/cimv2"
sQuery = "SELECT State FROM Win32_Service where Name = """ &amp; EscapeWQLString(sServiceName) &amp; """"

Set oWMI = GetObject(sNamespace)
Set objClasses = oWMI.ExecQuery(sQuery)

if objClasses.Count &gt;= 1 Then
sState = GetFirstItemFromWMIQuery(objClasses).Properties_.Item("State")
End If

If Err.number &lt;&gt; 0 Or objClasses.Count = 0 Then
sState = "Unknown"
End If

Err.Clear
GetServiceState = sState
End Function
'#Include File:SQL2014Constants.vbs

Const SQL_WMI_NAMESPACE = "ComputerManagement12"
'#Include File:Error.vbs

Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile

Public Sub Save()
m_lNumber = Err.Number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.HelpFile
End Sub

Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub

Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub

Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class

Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
On Error Resume Next
Dim oAPITemp
Set oAPITemp = MOMCreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent WScript.ScriptName, 4201, 1, sMessage &amp; ". " &amp; oErr.Description
End Function

Function ThrowScriptError(Byval sMessage, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function

Sub HandleError(customMessage)
Dim localLogger
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Wscript.Quit 0
End If
End Sub

Function HandleErrorContinue(customMessage)
Dim localLogger
HandleErrorContinue = False
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Err.Clear
HandleErrorContinue = True
End If
End Function

'#Include File:WMI.vbs

Function EscapeWQLString (ByVal strValue)
On Error Resume Next
Err.Clear
EscapeWQLString = Replace(strValue, "'", "\'")
End Function

Function WMIGetProperty(oWmi, sPropName, nCIMType, ErrAction)
Dim sValue, oWmiProp

If Not IsValidObject(oWmi) Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "Accessing property on invalid WMI object.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()

WMIGetProperty = ""
Exit Function
End If

On Error Resume Next
Set oWmiProp = oWmi.Properties_.Item(sPropName)
If Err.Number &lt;&gt; 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" &amp; sPropName &amp; "'.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
End If
On Error GoTo 0

If IsValidObject(oWmiProp) Then
sValue = oWmiProp.Value

If IsNull(sValue) Then
'
' If value is null, return blank to avoid any issues
'
WMIGetProperty = ""

Else

Select Case (oWmiProp.CIMType)
Case wbemCimtypeString, wbemCimtypeSint16, wbemCimtypeSint32, wbemCimtypeReal32, wbemCimtypeReal64, wbemCimtypeSint8, wbemCimtypeUint8, wbemCimtypeUint16, wbemCimtypeUint32, wbemCimtypeSint64, wbemCimtypeUint64:
If Not oWmiProp.IsArray Then
WMIGetProperty = Trim(CStr(sValue))
Else
WMIGetProperty = Join(sValue, ", ")
End If

Case wbemCimtypeBoolean:
If sValue = 1 Or UCase(sValue) = "TRUE" Then
WMIGetProperty = "True"
Else
WMIGetProperty = "False"
End If

Case wbemCimtypeDatetime:
Dim sTmpStrDate

'
' First attempt to convert the whole wmi date string
'
sTmpStrDate = Mid(sValue, 5, 2) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
Left(sValue, 4) &amp; " " &amp; _
Mid (sValue, 9, 2) &amp; ":" &amp; _
Mid(sValue, 11, 2) &amp; ":" &amp; _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
WMIGetProperty = CDate(sTmpStrDate)
Else

'
' Second, attempt just to convert the YYYYMMDD
'
sTmpStrDate = Mid(sValue, 5, 2) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
Left(sValue, 4)
If IsDate(sTmpStrDate) Then
WMIGetProperty = CDate(sTmpStrDate)
Else
'
' Nothing works - return passed in string
'
WMIGetProperty = sValue
End If

End If

Case Else:
WMIGetProperty = ""
End Select
End If
Else

If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" &amp; sPropName &amp; "'.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()

WMIGetProperty = ""

End If


If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.Echo " + " &amp; sPropName &amp; " :: '" &amp; WMIGetProperty &amp; "'"

End Function

Function WMIExecQuery(ByVal sNamespace, ByVal sQuery)
'
' WMIExecQuery :: Executes the WMI query and returns the result set.
'
'
Dim oWMI, oQuery, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error GoTo 0
If IsEmpty(oWMI) Then
ThrowScriptErrorNoAbort "Unable to open WMI Namespace '" &amp; sNamespace &amp; "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
ThrowEmptyDiscoveryData
End If

On Error Resume Next
Set oQuery = oWMI.ExecQuery(sQuery)
e.Save
On Error GoTo 0
If IsEmpty(oQuery) Or e.Number &lt;&gt; 0 Then
ThrowScriptError "The Query '" &amp; sQuery &amp; "' returned an invalid result set. Please check to see if this is a valid WMI Query.", e
End If

'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oQuery.Count
e.Save
On Error GoTo 0
If e.Number &lt;&gt; 0 Then
ThrowScriptError "The Query '" &amp; sQuery &amp; "' did not return any valid instances. Please check to see if this is a valid WMI Query.", e
End If

Set WMIExecQuery = oQuery

End Function

Function GetFirstItemFromWMIQuery(ByRef oQuery)
ON ERROR RESUME NEXT
Err.Clear
Dim oResult
Set oResult = oQuery.ItemIndex(0)
if Err.number &lt;&gt; 0 then
Err.Clear
Dim oObject
For Each oObject in oQuery
Set oResult = oObject
Exit For
Next
end if
Set GetFirstItemFromWMIQuery = oResult
End Function
'#Include File:ConnectionString.vbs

Function BuildConnectionString(strServer, strDatabase)
ON ERROR RESUME NEXT
Err.Clear

Dim dataSource
dataSource = BuildServerName(strServer, "")
BuildConnectionString = "Data Source=" &amp; EscapeConnStringValue(dataSource) &amp; ";Initial Catalog=" &amp; EscapeConnStringValue(strDatabase) &amp; ";Integrated Security=SSPI"
End Function

Function BuildConnectionStringWithPort(ByVal strServer, ByVal strDatabase, ByVal tcpPort)
ON ERROR RESUME NEXT
Err.Clear

Dim dataSource
dataSource = strServer
If ((tcpPort &lt;&gt; "0") And (tcpPort &lt;&gt; "")) Then
dataSource = dataSource &amp; "," &amp; tcpPort
End If
BuildConnectionStringWithPort = "Data Source=" &amp; EscapeConnStringValue(dataSource) &amp; ";Initial Catalog=" &amp; EscapeConnStringValue(strDatabase) &amp; ";Integrated Security=SSPI"
End Function

' This function should be used to escape Connection String keywords.
Function EscapeConnStringValue (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear

EscapeConnStringValue = """" + Replace(strValue, """", """""") + """"
End Function

Function EscapeWQLString (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear

EscapeWQLString = Replace(strValue, "'", "\'")
End Function

Function GetTcpPort (ByVal strServer)
ON ERROR RESUME NEXT
Err.Clear

Dim tcpPort
tcpPort = ""

Call BuildServerName(strServer, tcpPort)

GetTcpPort = tcpPort

End Function

Function BuildServerName(ByVal strServer, ByRef tcp)
ON ERROR RESUME NEXT
Err.Clear

Dim pathArray, instanceName, computerName, ip, serverName
Dim oWMI, oQuery

ip= ""

pathArray = Split(strServer, "\")
computerName = pathArray(0)
instanceName = "MSSQLSERVER"
if (pathArray.Count &gt; 1) Then
instanceName = pathArray(1)
End If

serverName = strServer

Set oWMI = GetObject("winmgmts:\\" &amp; computerName &amp; "\root\Microsoft\SqlServer\ComputerManagement12")



Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"&amp; EscapeWQLString(instanceName) &amp;"' AND PropertyName = 'ListenOnAllIPs'")

If oQuery.Count &gt;0 Then
Dim isListenAll
Set isListenAll = GetFirstItemFromWMIQuery(oQuery)
If(isListenAll.PropertyNumVal = 1) Then
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"&amp; EscapeWQLString(instanceName) &amp;"' AND IPAddressName = 'IPAll' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal &lt;&gt; ''")

If (oQuery.Count &gt; 0) Then
tcp = GetFirstItemFromWMIQuery(oQuery).PropertyStrVal

If ((tcp &lt;&gt; "0") And (tcp &lt;&gt; "")) Then
serverName = serverName &amp; "," &amp; tcp
Else tcp = ""
End If
End If
Else
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"&amp; EscapeWQLString(instanceName) &amp;"' AND IPAddressName &lt;&gt; '' AND PropertyName = 'Enabled' AND PropertyNumVal = 1")
If (oQuery.Count &gt; 0) Then
Dim ipAddressName
ipAddressName = GetFirstItemFromWMIQuery(oQuery).IPAddressName
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"&amp; EscapeWQLString(instanceName) &amp;"' AND IPAddressName = '"&amp; EscapeWQLString(ipAddressName) &amp;"' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal &lt;&gt; ''")
If (oQuery.Count &gt; 0) Then
tcp = GetFirstItemFromWMIQuery(oQuery).PropertyStrVal
End If
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"&amp; EscapeWQLString(instanceName) &amp;"' AND IPAddressName = '"&amp; EscapeWQLString(ipAddressName) &amp;"' AND PropertyName = 'IpAddress' AND PropertyStrVal &lt;&gt; ''")
If (oQuery.Count &gt; 0) Then
ip = GetFirstItemFromWMIQuery(oQuery).PropertyStrVal
End If
If ip &lt;&gt; "" Then
serverName = ip
End If
If ((tcp &lt;&gt; "0") And (tcp &lt;&gt; "")) Then
serverName = servername &amp; "," &amp; tcp
Else tcp = ""
End If
End If
End If
End If
On Error Goto 0
BuildServerName = serverName
End Function

Public Function IsValidDestination(dbConnection, serverName, instanceName, isADODB)
Dim destinationTestQuery
destinationTestQuery = "select SERVERPROPERTY('MachineName') as ServerName, @@servicename as InstanceName"
if 0 = Err.number then
Dim queryResult
if isADODB then
Set queryResult = dbConnection.ExecuteQuery(destinationTestQuery)
else
Set queryResult = dbConnection.Execute(destinationTestQuery)
end if
if Not queryResult.EOF then
Dim queryServerName : queryServerName = UCase(queryResult("ServerName").Value)
Dim queryInstanceName : queryInstanceName = UCase(queryResult("InstanceName").Value)
Dim serverNameWithoutDomain : serverNameWithoutDomain = serverName
Dim dotPosition : dotPosition = InStr(1, serverName, ".")
if Not IsNull(dotPosition) And (dotPosition &gt; 0) then
serverNameWithoutDomain = Left(serverName, dotPosition - 1)
end if
if (UCase(serverNameWithoutDomain) = queryServerName) And (UCase(instanceName) = queryInstanceName) then
IsValidDestination = true
Exit Function
end if
end if
end if
IsValidDestination = false
End Function

'NOTE: This will NOT work without SQLADODB.vbs /DKalinin/
'RETURNS:
Public Function SmartConnect(cnADOConnection, connectionStr, tcp, serverName, instanceName, databaseName)
ON ERROR RESUME NEXT
'try to use SQL server browser
Dim strProv : strProv = BuildConnectionStringWithPort(connectionStr, databaseName, "")
Err.Clear
Dim res : res = cnADOConnection.Open(strProv, "sqloledb", 10)
'use original tcp port and try to connect again
if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, true)) then
strProv = BuildConnectionStringWithPort(connectionStr, databaseName, tcp)
Err.Clear
res = cnADOConnection.Open(strProv, "sqloledb", 10)
'get fresh tcp port and try to connect again
if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, true)) then
Err.Clear
strProv = BuildConnectionString(connectionStr, databaseName)
res = cnADOConnection.Open(strProv, "sqloledb", 30)

if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, true)) then
cnADOConnection.HandleOpenConnectionErrorContinue databaseName, serverName, instanceName
SmartConnect = False
Exit Function
end if
end if
end if
SmartConnect = res
End Function


'NOTE: This WILL work without SQLADODB.vbs /DKalinin/
Public Function SmartConnectWithoutSQLADODB(connectionStr, tcp, serverName, instanceName, databaseName)
ON ERROR RESUME NEXT
Dim cnADOConnection
Set cnADOConnection = MomCreateObject("ADODB.Connection")
cnADOConnection.Provider = "sqloledb"
cnADOConnection.ConnectionTimeout = 30
'try to use SQL server browser
Dim strProv : strProv = BuildConnectionStringWithPort(connectionStr, databaseName, "")
Err.Clear
cnADOConnection.Open strProv
'use original tcp port and try to connect again
if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, false)) then
Err.Clear
strProv = BuildConnectionStringWithPort(connectionStr, databaseName, tcp)
cnADOConnection.Open strProv
'get fresh tcp port and try to connect again
if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, false)) then
Err.Clear
strProv = BuildConnectionString(connectionStr, databaseName)
cnADOConnection.Open strProv

if (0 &lt;&gt; Err.number) Or (Not IsValidDestination(cnADOConnection, serverName, instanceName, false)) then
cnADOConnection.HandleOpenConnectionErrorContinue databaseName, serverName, instanceName
Set SmartConnectWithoutSQLADODB = Nothing
Exit Function
end if
end if
end if
Set SmartConnectWithoutSQLADODB = cnADOConnection
End Function

Function furlEncode(vString,vEncDec)
Dim i
Dim aReserved(24,1)
'column 1
aReserved(0,0) = "%" '25
aReserved(1,0) = ";" '3B
aReserved(2,0) = "/" '2F
aReserved(3,0) = "?" '3F
aReserved(4,0) = ":" '3A
aReserved(5,0) = "@" '40
aReserved(6,0) = "&amp;" '26
aReserved(7,0) = "=" '3D
aReserved(8,0) = "+" '2B
aReserved(9,0) = "$" '24
aReserved(10,0) = "," '2C
aReserved(11,0) = " " '20
aReserved(12,0) = """" '22
aReserved(13,0) = "&lt;" '3C
aReserved(14,0) = "&gt;" '3E
aReserved(15,0) = "#" '23
aReserved(16,0) = "{" '7B
aReserved(17,0) = "}" '7D
aReserved(18,0) = "|" '7C
aReserved(19,0) = "\" '5C
aReserved(20,0) = "^" '5E
aReserved(21,0) = "~" '7E
aReserved(22,0) = "[" '5B
aReserved(23,0) = "]" '5D
aReserved(24,0) = "`" '60
'column 2
aReserved(0,1) = "%25"
aReserved(1,1) = "%3B"
aReserved(2,1) = "%2F"
aReserved(3,1) = "%3F"
aReserved(4,1) = "%3A"
aReserved(5,1) = "%40"
aReserved(6,1) = "%26"
aReserved(7,1) = "%3D"
aReserved(8,1) = "%2B"
aReserved(9,1) = "%24"
aReserved(10,1) = "%2C"
aReserved(11,1) = "%20"
aReserved(12,1) = "%22"
aReserved(13,1) = "%3C"
aReserved(14,1) = "%3E"
aReserved(15,1) = "%23"
aReserved(16,1) = "%7B"
aReserved(17,1) = "%7D"
aReserved(18,1) = "%7C"
aReserved(19,1) = "%5C"
aReserved(20,1) = "%5E"
aReserved(21,1) = "%7E"
aReserved(22,1) = "%5B"
aReserved(23,1) = "%5D"
aReserved(24,1) = "%60"

For i = 0 to Ubound(aReserved)
If vEncDec = "enc" Then
vString = Replace(vString,aReserved(i,0),aReserved(i,1))
End If
If vEncDec = "dec" Then
vString = Replace(vString,aReserved(i,1),aReserved(i,0))
End If
Next

furlEncode = vString

End Function'#Include File:SQLBlockingSPIDsMonitoring.vbs

dim ERROR_EVENT

' MOM.ScriptAPI takes 3rd parameter as severity of the event
ERROR_EVENT = 1

Dim MAXINT_4
MAXINT_4 = 2147483647

'Start of Main
'-----------------------------------------------------------------------------------------------
call Main()

Sub Main()

Dim objParameters, sConnectionString, sTcpPort
Dim oAPI, oBag
Dim iTime
Dim sInstanceName, sComputerName

Set objParameters = WScript.Arguments

If objParameters.Count &lt;&gt; 5 Then
Quit()
End If

sConnectionString= objParameters(0)
iTime = objParameters(1)
sInstanceName = objParameters(2)
sComputerName = objParameters(3)
sTcpPort = objParameters(4)

If CDbl(iTime) &lt; 0 Or CDbl(iTime) &gt; (MAXINT_4 / (1000 * 60)) Then
WriteToEventLogAndExit("Invalid WaitMinutes parameter in BlockingSPIDsProvider. Aborting.")
End If

Set objParameters = Nothing

Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()

If CheckBlockedSPIDS(oBag, sConnectionString, iTime,sInstanceName, sComputerName, sTcpPort) = 0 Then
Call oAPI.Return(oBag)
Else
Quit()
End If

End Sub
'End of Main
'-----------------------------------------------------------------------------------------------

Sub WriteToEventLogAndExit(ByVal message)
Dim oAPITemp
Set oAPITemp = CreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent WScript.ScriptName, 4201, ERROR_EVENT, message

WScript.Quit()
End Sub

Function EscapeWQLString (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear

EscapeWQLString = Replace(strValue, "'", "\'")
End Function

Function IsServiceRunning(sqlInstanceName)
Dim oWMI, oQuery, res
res = false

On Error Resume Next
Set oWMI = GetObject("winmgmts://./root/Microsoft/SqlServer/" &amp; SQL_WMI_NAMESPACE)
Set oQuery = oWMI.ExecQuery("select state from SqlService WHERE (ServiceName = '"&amp; EscapeWQLString(sqlInstanceName) &amp;"' OR ServiceName = 'MSSQL$" &amp; EscapeWQLString(sqlInstanceName) &amp;"') and SQLServiceType ='1'")

If oQuery.Count &gt; 0 Then
Dim sqlServiceObject
For Each sqlServiceObject In oQuery
If sqlServiceObject.State = 4 Then
res = true
Exit For
End If
Next
End If
On Error Goto 0

IsServiceRunning = res

End Function

Function PushCellToTable(table, name, rsBlockedSPIDS)
dim value
If IsNull(rsBlockedSPIDS(name).Value) Then
value = "NULL"
Else
value = rsBlockedSPIDS(name).Value
End If
table = table &amp; "&lt;" &amp; name &amp; "&gt;&lt;![CDATA" &amp; "[" &amp; value &amp; "]" &amp; "]&gt;&lt;/" &amp; name &amp; "&gt;"
PushCellToTable = table
End Function

Function CheckBlockedSPIDS(ByRef oBag, ByVal sConnectionString, ByVal iWaitInMinutes, ByRef sInstanceName, ByRef sComputerName, ByVal sTcpPort)
dim cnADOConnection
dim rsBlockedSPIDS
dim sName
dim bFail
dim SCRIPT_SQL
dim SPIDList
dim SPIDCount
dim blokedSPID_TableRow

SCRIPT_SQL = "SET NOCOUNT ON " &amp; vbCrLf &amp; _
"DECLARE @wait_threshold INT " &amp; vbCrLf &amp; _
"SET @wait_threshold = 1000 * 60 * ? " &amp; vbCrLf &amp; _
"DECLARE @servermajorversion INT " &amp; vbCrLf &amp; _
"SET @servermajorversion = REPLACE (LEFT (CONVERT (varchar, SERVERPROPERTY ('ProductVersion')), 2), '.', '') " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"IF OBJECT_ID ('tempdb.dbo.#tmp_blockers') IS NOT NULL DROP TABLE #tmp_blockers " &amp; vbCrLf &amp; _
"IF OBJECT_ID ('tempdb.dbo.#tmp_head_blockers') IS NOT NULL DROP TABLE #tmp_head_blockers " &amp; vbCrLf &amp; _
"IF OBJECT_ID ('tempdb.dbo.#tmp_head_blocker_depth') IS NOT NULL DROP TABLE #tmp_head_blocker_depth " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"SELECT " &amp; vbCrLf &amp; _
" S.session_id, " &amp; vbCrLf &amp; _
" CASE " &amp; vbCrLf &amp; _
" WHEN R.blocking_session_id IS NULL OR R.blocking_session_id = 0 THEN 'TRUE' " &amp; vbCrLf &amp; _
" ELSE 'FALSE' " &amp; vbCrLf &amp; _
" END AS head_blocker, " &amp; vbCrLf &amp; _
" R.blocking_session_id, " &amp; vbCrLf &amp; _
" R.status AS request_status, " &amp; vbCrLf &amp; _
" S.status AS session_status, " &amp; vbCrLf &amp; _
" CASE R.sql_handle " &amp; vbCrLf &amp; _
" WHEN NULL THEN " &amp; vbCrLf &amp; _
" (SELECT text FROM sys.dm_exec_sql_text(R.sql_handle)) " &amp; vbCrLf &amp; _
" ELSE " &amp; vbCrLf &amp; _
" (SELECT text FROM sys.dm_exec_sql_text(C.most_recent_sql_handle)) " &amp; vbCrLf &amp; _
" END AS sql_stmnt, " &amp; vbCrLf &amp; _
" S.program_name, " &amp; vbCrLf &amp; _
" S.host_name, " &amp; vbCrLf &amp; _
" S.host_process_id, " &amp; vbCrLf &amp; _
" S.is_user_process, " &amp; vbCrLf &amp; _
" S.login_name, " &amp; vbCrLf &amp; _
" S.login_time, " &amp; vbCrLf &amp; _
" R.start_time AS request_start_time, " &amp; vbCrLf &amp; _
" R.wait_type, " &amp; vbCrLf &amp; _
" R.last_wait_type, " &amp; vbCrLf &amp; _
" CONVERT(NUMERIC(9,3),(R.wait_time / 1000.0)) AS wait_time_in_sec, " &amp; vbCrLf &amp; _
" R.command, " &amp; vbCrLf &amp; _
" R.wait_resource, " &amp; vbCrLf &amp; _
" CASE COALESCE(R.transaction_isolation_level, S.transaction_isolation_level) " &amp; vbCrLf &amp; _
" WHEN 0 THEN '0-Unspecified' " &amp; vbCrLf &amp; _
" WHEN 1 THEN '1-ReadUncomitted' " &amp; vbCrLf &amp; _
" WHEN 2 THEN '2-ReadCommitted' " &amp; vbCrLf &amp; _
" WHEN 3 THEN '3-Repeatable' " &amp; vbCrLf &amp; _
" WHEN 4 THEN '4-Serializable' " &amp; vbCrLf &amp; _
" WHEN 5 THEN '5-Snapshot' " &amp; vbCrLf &amp; _
" ELSE CONVERT(VARCHAR(10), COALESCE(R.transaction_isolation_level, S.transaction_isolation_level)) + '-Unknown' " &amp; vbCrLf &amp; _
" END AS transaction_isolation_level, " &amp; vbCrLf &amp; _
" --SQLBUD #487091 " &amp; vbCrLf &amp; _
" CASE " &amp; vbCrLf &amp; _
" WHEN R.open_transaction_count IS NULL THEN (SELECT open_tran FROM sys.sysprocesses AS SP WHERE SP.spid = S.session_id) " &amp; vbCrLf &amp; _
" ELSE R.open_transaction_count " &amp; vbCrLf &amp; _
" END AS open_transaction_count, " &amp; vbCrLf &amp; _
" R.open_resultset_count, " &amp; vbCrLf &amp; _
" CONVERT (decimal(5,2), R.percent_complete) AS percent_complete, " &amp; vbCrLf &amp; _
" R.estimated_completion_time, " &amp; vbCrLf &amp; _
" --SQLBUD #438189 (fixed in SP2) " &amp; vbCrLf &amp; _
" CASE WHEN (@servermajorversion &gt; 9) OR (@servermajorversion = 9 AND SERVERPROPERTY ('ProductLevel') &gt;= 'SP2' COLLATE Latin1_General_BIN) " &amp; vbCrLf &amp; _
" THEN R.logical_reads ELSE R.logical_reads - S.logical_reads END AS request_logical_reads, " &amp; vbCrLf &amp; _
" CASE WHEN (@servermajorversion &gt; 9) OR (@servermajorversion = 9 AND SERVERPROPERTY ('ProductLevel') &gt;= 'SP2' COLLATE Latin1_General_BIN) " &amp; vbCrLf &amp; _
" THEN R.reads ELSE R.reads - S.reads END AS request_reads, " &amp; vbCrLf &amp; _
" CASE WHEN (@servermajorversion &gt; 9) OR (@servermajorversion = 9 AND SERVERPROPERTY ('ProductLevel') &gt;= 'SP2' COLLATE Latin1_General_BIN) " &amp; vbCrLf &amp; _
" THEN R.writes ELSE R.writes - S.writes END AS request_writes, " &amp; vbCrLf &amp; _
" R.cpu_time AS request_cpu_time, " &amp; vbCrLf &amp; _
" R.lock_timeout, " &amp; vbCrLf &amp; _
" R.deadlock_priority, " &amp; vbCrLf &amp; _
" R.row_count AS request_row_count, " &amp; vbCrLf &amp; _
" R.prev_error AS request_prev_error, " &amp; vbCrLf &amp; _
" R.nest_level, " &amp; vbCrLf &amp; _
" R.granted_query_memory, " &amp; vbCrLf &amp; _
" R.user_id, " &amp; vbCrLf &amp; _
" R.transaction_id, " &amp; vbCrLf &amp; _
" S.cpu_time AS session_cpu_time, " &amp; vbCrLf &amp; _
" S.memory_usage, " &amp; vbCrLf &amp; _
" S.reads AS session_reads, " &amp; vbCrLf &amp; _
" S.logical_reads AS session_logical_reads, " &amp; vbCrLf &amp; _
" S.writes AS session_writes, " &amp; vbCrLf &amp; _
" S.prev_error AS session_prev_error, " &amp; vbCrLf &amp; _
" S.row_count AS session_row_count " &amp; vbCrLf &amp; _
"INTO " &amp; vbCrLf &amp; _
" #tmp_blockers " &amp; vbCrLf &amp; _
"FROM " &amp; vbCrLf &amp; _
" (sys.dm_exec_sessions AS S " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_exec_requests AS R ON R.session_id = S.session_id) " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_exec_connections AS C ON C.session_id = S. session_id " &amp; vbCrLf &amp; _
"WHERE " &amp; vbCrLf &amp; _
" ( --Active Request " &amp; vbCrLf &amp; _
" R.session_id IS NOT NULL AND " &amp; vbCrLf &amp; _
" R.blocking_session_id != 0 AND " &amp; vbCrLf &amp; _
" S.session_id != @@SPID AND " &amp; vbCrLf &amp; _
" R.wait_time &gt; @wait_threshold AND " &amp; vbCrLf &amp; _
" (S.is_user_process = 1 OR R.status COLLATE Latin1_General_BIN NOT IN ('background', 'sleeping'))) " &amp; vbCrLf &amp; _
" OR --Head Blocker " &amp; vbCrLf &amp; _
" (S.session_id IN " &amp; vbCrLf &amp; _
" (SELECT S.session_id " &amp; vbCrLf &amp; _
" FROM sys.dm_exec_sessions AS S " &amp; vbCrLf &amp; _
" INNER JOIN sys.dm_exec_requests AS BER ON BER.blocking_session_id = S.session_id " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_exec_requests AS ER ON ER.session_id = S.session_id " &amp; vbCrLf &amp; _
" WHERE " &amp; vbCrLf &amp; _
" (ER.blocking_session_id = 0 OR ER.blocking_session_id IS NULL) " &amp; vbCrLf &amp; _
" AND BER.wait_time &gt; @wait_threshold)); " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"--Find Blocking Levels " &amp; vbCrLf &amp; _
";WITH blocking_levels(session_id, blocking_session_id, blocking_level, head_blocker) AS " &amp; vbCrLf &amp; _
"( " &amp; vbCrLf &amp; _
" SELECT session_id, blocking_session_id, 0 AS blocking_level, session_id AS head_blocker " &amp; vbCrLf &amp; _
" FROM #tmp_blockers " &amp; vbCrLf &amp; _
" WHERE blocking_session_id IS NULL OR blocking_session_id = 0 " &amp; vbCrLf &amp; _
" UNION ALL " &amp; vbCrLf &amp; _
" SELECT TB.session_id, TB.blocking_session_id, BL.blocking_level + 1 AS blocking_level, BL.head_blocker " &amp; vbCrLf &amp; _
" FROM #tmp_blockers AS TB " &amp; vbCrLf &amp; _
" INNER JOIN blocking_levels AS BL " &amp; vbCrLf &amp; _
" ON TB.blocking_session_id = BL.session_id " &amp; vbCrLf &amp; _
") " &amp; vbCrLf &amp; _
"SELECT * " &amp; vbCrLf &amp; _
"INTO #tmp_head_blockers " &amp; vbCrLf &amp; _
"FROM blocking_levels " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"SELECT COUNT(*) - 1 AS head_blocking_depth, head_blocker " &amp; vbCrLf &amp; _
"INTO #tmp_head_blocker_depth " &amp; vbCrLf &amp; _
"FROM #tmp_head_blockers " &amp; vbCrLf &amp; _
"GROUP BY head_blocker " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"-- This query could be collapsed into the query above. It is broken out here to avoid an excessively " &amp; vbCrLf &amp; _
"-- large memory grant due to poor cardinality estimates (no stats on many DMVs). " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"SELECT TOP 20 " &amp; vbCrLf &amp; _
" TB.session_id, " &amp; vbCrLf &amp; _
" TB.blocking_session_id, " &amp; vbCrLf &amp; _
" THB.blocking_level, " &amp; vbCrLf &amp; _
" TB.head_blocker, " &amp; vbCrLf &amp; _
" THBD.head_blocking_depth, " &amp; vbCrLf &amp; _
" TB.request_status, " &amp; vbCrLf &amp; _
" TB.session_status, " &amp; vbCrLf &amp; _
" TB.sql_stmnt, " &amp; vbCrLf &amp; _
" TB.request_start_time, " &amp; vbCrLf &amp; _
" TB.wait_type, " &amp; vbCrLf &amp; _
" TB.last_wait_type, " &amp; vbCrLf &amp; _
" TB.wait_time_in_sec, " &amp; vbCrLf &amp; _
" TB.command, " &amp; vbCrLf &amp; _
" TB.program_name, " &amp; vbCrLf &amp; _
" TB.host_name, " &amp; vbCrLf &amp; _
" TB.host_process_id, " &amp; vbCrLf &amp; _
" TB.is_user_process, " &amp; vbCrLf &amp; _
" TB.login_name, " &amp; vbCrLf &amp; _
" TB.login_time, " &amp; vbCrLf &amp; _
" TB.wait_resource, " &amp; vbCrLf &amp; _
" TB.transaction_isolation_level, " &amp; vbCrLf &amp; _
" TB.open_transaction_count, " &amp; vbCrLf &amp; _
" TB.open_resultset_count, " &amp; vbCrLf &amp; _
" COALESCE(AT.name, AT2.name) AS transaction_name, " &amp; vbCrLf &amp; _
" COALESCE(AT.transaction_begin_time, AT2.transaction_begin_time) AS transaction_begin_time, " &amp; vbCrLf &amp; _
" CASE COALESCE(AT.transaction_type, AT2.transaction_type) " &amp; vbCrLf &amp; _
" WHEN 1 THEN '1-Read/write transaction' " &amp; vbCrLf &amp; _
" WHEN 2 THEN '2-Read-only transaction' " &amp; vbCrLf &amp; _
" WHEN 3 THEN '3-System transaction' " &amp; vbCrLf &amp; _
" WHEN 4 THEN '4-Distributed transaction' " &amp; vbCrLf &amp; _
" ELSE CONVERT(VARCHAR(10), COALESCE(AT.transaction_type, AT2.transaction_type)) + '-Unknown' " &amp; vbCrLf &amp; _
" END AS transaction_type, " &amp; vbCrLf &amp; _
" CASE COALESCE(AT.transaction_state, AT2.transaction_state) " &amp; vbCrLf &amp; _
" WHEN 0 THEN '0-The transaction has not been completely initialized yet.' " &amp; vbCrLf &amp; _
" WHEN 1 THEN '1-The transaction has been initialized but has not started.' " &amp; vbCrLf &amp; _
" WHEN 2 THEN '2-The transaction is active.' " &amp; vbCrLf &amp; _
" WHEN 3 THEN '3-The transaction has ended. This is used for read-only transactions.' " &amp; vbCrLf &amp; _
" WHEN 4 THEN '4-The commit process has been initiated on the distributed transaction. This is for distributed transactions only. The distributed transaction is still active but further processing cannot take place.' " &amp; vbCrLf &amp; _
" WHEN 5 THEN '5-The transaction is in a prepared state and waiting resolution.' " &amp; vbCrLf &amp; _
" WHEN 6 THEN '6-The transaction has been committed.' " &amp; vbCrLf &amp; _
" WHEN 7 THEN '7-The transaction is being rolled back.' " &amp; vbCrLf &amp; _
" WHEN 8 THEN '8-The transaction has been rolled back.' " &amp; vbCrLf &amp; _
" ELSE CONVERT(VARCHAR(10), COALESCE(AT.transaction_state, AT2.transaction_state)) + '-Unknown' " &amp; vbCrLf &amp; _
" END AS transaction_state, " &amp; vbCrLf &amp; _
" TB.percent_complete, " &amp; vbCrLf &amp; _
" TB.estimated_completion_time, " &amp; vbCrLf &amp; _
" TB.request_logical_reads, " &amp; vbCrLf &amp; _
" TB.request_reads, " &amp; vbCrLf &amp; _
" TB.request_writes, " &amp; vbCrLf &amp; _
" TB.request_cpu_time, " &amp; vbCrLf &amp; _
" TB.lock_timeout, " &amp; vbCrLf &amp; _
" TB.deadlock_priority, " &amp; vbCrLf &amp; _
" TB.request_row_count, " &amp; vbCrLf &amp; _
" TB.request_prev_error, " &amp; vbCrLf &amp; _
" TB.nest_level, " &amp; vbCrLf &amp; _
" TB.granted_query_memory, " &amp; vbCrLf &amp; _
" TB.user_id, " &amp; vbCrLf &amp; _
" TB.transaction_id, " &amp; vbCrLf &amp; _
" TB.session_cpu_time, " &amp; vbCrLf &amp; _
" TB.memory_usage, " &amp; vbCrLf &amp; _
" TB.session_reads, " &amp; vbCrLf &amp; _
" TB.session_logical_reads, " &amp; vbCrLf &amp; _
" TB.session_writes, " &amp; vbCrLf &amp; _
" TB.session_prev_error, " &amp; vbCrLf &amp; _
" TB.session_row_count " &amp; vbCrLf &amp; _
"FROM " &amp; vbCrLf &amp; _
" #tmp_blockers AS TB " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_tran_active_transactions AS AT ON AT.transaction_id = TB.transaction_id " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_tran_session_transactions AS TS ON TS.session_id = TB.session_id " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN sys.dm_tran_active_transactions AS AT2 ON AT2.transaction_id = TS.transaction_id " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN #tmp_head_blockers AS THB ON THB.session_id = TB.session_id " &amp; vbCrLf &amp; _
" LEFT OUTER JOIN #tmp_head_blocker_depth AS THBD ON THBD.head_blocker = TB.session_id " &amp; vbCrLf &amp; _
" " &amp; vbCrLf &amp; _
"ORDER BY TB.head_blocker DESC, THB.blocking_level"


On Error resume next

Set cnADOConnection = SmartConnectWithoutSQLADODB(sConnectionString, sTcpPort, sComputerName, sInstanceName, "master")
if cnADOConnection Is Nothing Then
If (((Err.number and 65535) = 16389) or ((Err.number and 65535) = 3661)) And IsServiceRunning(sInstanceName) then ' Login failed or SQL Server does not exist or access denied.
ThrowScriptError "Cannot login to database [" &amp; sComputerName &amp; "][" &amp; sInstanceName &amp; ":master] ", Err
End If
Exit Function
End If

Err.Clear

Dim Cmd
Set Cmd = CreateObject("ADODB.Command")
' Specify the connection
Cmd.ActiveConnection = cnADOConnection
' Specify command type and text
Cmd.CommandText = SCRIPT_SQL
Cmd.CommandType = 1 ' adCmdText
AddParam Cmd, iWaitInMinutes

Set rsBlockedSPIDS = Cmd.Execute
if 0 &lt;&gt; Err.number then
Exit Function
end if

SPIDList = ""
SPIDCount = 0
blokedSPID_TableRow = "&lt;?xml version=""1.0"" encoding=""utf-8""?&gt;&lt;BlockedSPIDTable&gt;"
do
bFail = True
Err.Clear
if rsBlockedSPIDS.EOF then
if 0 &lt;&gt; Err.number then Exit Do
bFail = False
Exit Do
end if
if 0 &lt;&gt; Err.number then Exit Do

' We don't want to show Head Blockers in list of blocked
If rsBlockedSPIDS("head_blocker").Value = "FALSE" Then
SPIDCount = SPIDCount + 1

if SPIDList = "" Then
SPIDList = CStr(rsBlockedSPIDS("session_id").Value)
else
SPIDList = SPIDList &amp; "," &amp; CStr(rsBlockedSPIDS("session_id").Value)
end if
End If

blokedSPID_TableRow = blokedSPID_TableRow &amp; "&lt;session&gt;"
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_id", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "blocking_session_id", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "blocking_level", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "head_blocker", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "head_blocking_depth", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_status", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_status", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "sql_stmnt", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_start_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "wait_type", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "last_wait_type", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "wait_time_in_sec", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "program_name", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "host_name", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "host_process_id", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "is_user_process", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "login_name", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "login_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "wait_resource", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_isolation_level", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "open_transaction_count", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "open_resultset_count", rsBlockedSPIDS)

blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_name", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_begin_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_type", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_state", rsBlockedSPIDS)

blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "percent_complete", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "estimated_completion_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_logical_reads", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_reads", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_writes", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_cpu_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "lock_timeout", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "deadlock_priority", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_row_count", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "request_prev_error", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "nest_level", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "granted_query_memory", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "user_id", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "transaction_id", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_cpu_time", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "memory_usage", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_reads", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_logical_reads", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_writes", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_prev_error", rsBlockedSPIDS)
blokedSPID_TableRow = PushCellToTable(blokedSPID_TableRow, "session_row_count", rsBlockedSPIDS)
blokedSPID_TableRow = blokedSPID_TableRow &amp; "&lt;/session&gt;"

Err.Clear
rsBlockedSPIDS.MoveNext
if 0 &lt;&gt; Err.number then Exit Do
Loop
blokedSPID_TableRow = blokedSPID_TableRow &amp; "&lt;/BlockedSPIDTable&gt;"

Call oBag.AddValue("BlockedSPIDTable",CStr(blokedSPID_TableRow))
Call oBag.AddValue("BlockedSPIDList",CStr(SPIDList))
Call oBag.AddValue("BlockedSPIDCount",CInt(SPIDCount))

if bFail then
Exit Function
end if

Set cnADOConnection = Nothing
Set rsBlockedSPIDS = Nothing

CheckBlockedSPIDS = 0
End Function

Sub AddParam(cmd, value)
Dim Parameter
Select Case VarType(value)
Case 0 ' Empty
Set Parameter = cmd.CreateParameter(, 0, 1) ' , adEmpty, adParamInput
Case 1 ' Null
Set Parameter = cmd.CreateParameter(, 0, 1) ' , adEmpty, adParamInput
Case 2 ' int
Set Parameter = cmd.CreateParameter(, 3, 1, , value) ' , adInteger, adParamInput
Case 3 ' long
Set Parameter = cmd.CreateParameter(, 20, 1, , value) ' , adBigInt, adParamInput
Case 8 ' string
Set Parameter = cmd.CreateParameter(, 202, 1, Len(value), value) ' , adVarWChar, adParamInput
Case else
HandleError("Unknown parameter type: " &amp; VarType(value))
End Select
cmd.Parameters.Append Parameter
End Sub </Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>