달력

5

« 2024/5 »

  • 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
  • 30
  • 31
2010. 2. 26. 08:50

VB Script를 이용한 SQL Lock 확인 (VBS) Etc.2010. 2. 26. 08:50

' 파라미터는 데이터베이스 이름
Dim oArgs
Set oArgs = WScript.Arguments

If oArgs.Count < 1 Then
WScript.Quit -1
End If

Dim dbcon, strCon, strDB, strSQL
strDB = oArgs(0)

Set dbcon = createobject("adodb.connection")
strCon = "provider=sqloledb;Data Source=localhost;Initial Catalog="&strDB&";Integrated Security=SSPI"

dbcon.open strCon
strSQL ="SELECT L.request_session_id AS SPID,DB_NAME(L.resource_database_id) AS DatabaseName,O.Name AS LockedObjectName,ST.text AS SqlStatementText,ES.login_name AS LoginName,ES.host_name AS HostName FROM sys.dm_tran_locks L JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id JOIN sys.objects O ON O.object_id = P.object_id JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST WHERE resource_database_id = db_id() ORDER BY L.request_session_id"

Set Result = dbcon.execute(strSQL)

WScript.Echo "SPID | DatabaseName | LockedObjectName | SqlStatementText | LoginName | HostName"
Wscript.Echo "================================================================================"

Do while not Result.eof
strROW1 = Result(0)
strROW2 = Result(1)
strROW3 = Result(2)
strROW4 = Result(3)
strROW5 = Result(4)
strROW6 = Result(5)

If IsNull(strROW1) Then
strROW1 = "NULL"
End If
If IsNull(strROW2) Then
strROW2 = "NULL"
End If
If IsNull(strROW3) Then
strROW3 = "NULL"
End If
If IsNull(strROW4) Then
strROW4 = "NULL"
End If
If IsNull(strROW5) Then
strROW5 = "NULL"
End If
If IsNull(strROW6) Then
strROW6 = "NULL"
End If

WScript.Echo strROW1 & " | " & strROW2 & " | " & strROW3 & " | " & strROW4 & " | " & strROW5 & " | " & strROW6
Wscript.Echo "--------------------------------------------------------------------------------"

Result.movenext
loop

set Result = NOTHING
dbcon.close

:
Posted by 커널64