当前位置:Gxlcms > mysql > SQL中的事务处理机制--sp_lock2

SQL中的事务处理机制--sp_lock2

时间:2021-07-01 10:21:17 帮助过:72人阅读

SQL中的事务处理机制 事务处理 sp_lock2Version: SQL Server 7.0/2000Created by: Alexander Chigrikhttp://www.MSSQLCity.com/ - all about MS SQL(SQL Server Articles, FAQ, Scripts, Tips and Test Exams). This stored procedure can be used instead of

SQL中的事务处理机制 事务处理
  1. sp_lock2
  2. Version: SQL Server 7.0/2000
  3. Created by: Alexander Chigrik
  4. http://www.MSSQLCity.com/ - all about MS SQL
  5. (SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
  6. This stored procedure can be used instead of sp_lock system stored procedure
  7. to return more detailed locking view (it can return user name, host name,
  8. database name, object name, index name and object owner).
  9. This is the example to use sp_lock2:
  10. EXEC sp_lock2
  11. /*
  12. Version: SQL Server 7.0/2000
  13. Created by: Alexander Chigrik
  14. http://www.MSSQLCity.com/ - all about MS SQL
  15. (SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
  16. This stored procedure can be used instead of sp_lock stored procedure
  17. to return more detailed locking view (it can return user name, host name,
  18. database name, object name, index name and object owner).
  19. This is the example to use sp_lock2:
  20. EXEC sp_lock2
  21. */
  22. USE MASTER
  23. GO
  24. IF OBJECT_ID('sp_lock2') IS NOT NULL DROP PROC sp_lock2
  25. GO
  26. CREATE PROCEDURE sp_lock2
  27. @spid1 int = NULL, /* server process id to check for locks */
  28. @spid2 int = NULL /* other process id to check for locks */
  29. as
  30. set nocount on
  31. /*
  32. ** Show the locks for both parameters.
  33. */
  34. declare @objid int,
  35. @indid int,
  36. @dbid int,
  37. @string Nvarchar(255)
  38. CREATE TABLE #locktable
  39. (
  40. spid smallint
  41. ,loginname nvarchar(20)
  42. ,hostname nvarchar(30)
  43. ,dbid int
  44. ,dbname nvarchar(20)
  45. ,ObjOwner nvarchar(128)
  46. ,objId int
  47. ,ObjName nvarchar(128)
  48. ,IndId int
  49. ,IndName nvarchar(128)
  50. ,Type nvarchar(4)
  51. ,Resource nvarchar(16)
  52. ,Mode nvarchar(8)
  53. ,Status nvarchar(5)
  54. )
  55. if @spid1 is not NULL
  56. begin
  57. INSERT #locktable
  58. (
  59. spid
  60. ,loginname
  61. ,hostname
  62. ,dbid
  63. ,dbname
  64. ,ObjOwner
  65. ,objId
  66. ,ObjName
  67. ,IndId
  68. ,IndName
  69. ,Type
  70. ,Resource
  71. ,Mode
  72. ,Status
  73. )
  74. select convert (smallint, l.req_spid)
  75. ,coalesce(substring (s.loginame, 1, 20),'')
  76. ,coalesce(substring (s.hostname, 1, 30),'')
  77. ,l.rsc_dbid
  78. ,substring (db_name(l.rsc_dbid), 1, 20)
  79. ,''
  80. ,l.rsc_objid
  81. ,''
  82. ,l.rsc_indid
  83. ,''
  84. ,substring (v.name, 1, 4)
  85. ,substring (l.rsc_text, 1, 16)
  86. ,substring (u.name, 1, 8)
  87. ,substring (x.name, 1, 5)
  88. from master.dbo.syslockinfo l,
  89. master.dbo.spt_values v,
  90. master.dbo.spt_values x,
  91. master.dbo.spt_values u,
  92. master.dbo.sysprocesses s
  93. where l.rsc_type = v.number
  94. and v.type = 'LR'
  95. and l.req_status = x.number
  96. and x.type = 'LS'
  97. and l.req_mode + 1 = u.number
  98. and u.type = 'L'
  99. and req_spid in (@spid1, @spid2)
  100. and req_spid = s.spid
  101. end
  102. /*
  103. ** No parameters, so show all the locks.
  104. */
  105. else
  106. begin
  107. INSERT #locktable
  108. (
  109. spid
  110. ,loginname
  111. ,hostname
  112. ,dbid
  113. ,dbname
  114. ,ObjOwner
  115. ,objId
  116. ,ObjName
  117. ,IndId
  118. ,IndName
  119. ,Type
  120. ,Resource
  121. ,Mode
  122. ,Status
  123. )
  124. select convert (smallint, l.req_spid)
  125. ,coalesce(substring (s.loginame, 1, 20),'')
  126. ,coalesce(substring (s.hostname, 1, 30),'')
  127. ,l.rsc_dbid
  128. ,substring (db_name(l.rsc_dbid), 1, 20)
  129. ,''
  130. ,l.rsc_objid
  131. ,''
  132. ,l.rsc_indid
  133. ,''
  134. ,substring (v.name, 1, 4)
  135. ,substring (l.rsc_text, 1, 16)
  136. ,substring (u.name, 1, 8)
  137. ,substring (x.name, 1, 5)
  138. from master.dbo.syslockinfo l,
  139. master.dbo.spt_values v,
  140. master.dbo.spt_values x,
  141. master.dbo.spt_values u,
  142. master.dbo.sysprocesses s
  143. where l.rsc_type = v.number
  144. and v.type = 'LR'
  145. and l.req_status = x.number
  146. and x.type = 'LS'
  147. and l.req_mode + 1 = u.number
  148. and u.type = 'L'
  149. and req_spid = s.spid
  150. order by spID
  151. END
  152. DECLARE lock_cursor CURSOR
  153. FOR SELECT dbid, ObjId, IndId FROM #locktable
  154. WHERE Type <>'DB' and Type <> 'FIL'
  155. OPEN lock_cursor
  156. FETCH NEXT FROM lock_cursor INTO @dbid, @ObjId, @IndId
  157. WHILE @@FETCH_STATUS = 0
  158. BEGIN
  159. SELECT @string =
  160. 'USE ' + db_name(@dbid) + char(13)
  161. + 'update #locktable set ObjName = name, ObjOwner = USER_NAME(uid)'
  162. + ' from sysobjects where id = ' + convert(varchar(32),@objid)
  163. + ' and ObjId = ' + convert(varchar(32),@objid)
  164. + ' and dbid = ' + convert(varchar(32),@dbId)
  165. EXECUTE (@string)
  166. SELECT @string =
  167. 'USE ' + db_name(@dbid) + char(13)
  168. + 'update #locktable set IndName = i.name from sysindexes i '
  169. + ' where i.id = ' + convert(varchar(32),@objid)
  170. + ' and i.indid = ' + convert(varchar(32),@indid)
  171. + ' and ObjId = ' + convert(varchar(32),@objid)
  172. + ' and dbid = ' + convert(varchar(32),@dbId)
  173. + ' and #locktable.indid = ' + convert(varchar(32),@indid)
  174. EXECUTE (@string)
  175. FETCH NEXT FROM lock_cursor INTO @dbid, @ObjId, @IndId
  176. END
  177. CLOSE lock_cursor
  178. DEALLOCATE lock_cursor
  179. SELECT * FROM #locktable
  180. return (0)
  181. -- END sp_lock2
  182. GO

人气教程排行