set nocount on
declare @lock table
(
spid int,
dbid int,
objid int,
indid int,
type varchar(6),
resource varchar(max),
mode varchar(200),
status varchar(200)
)
declare @loclinfo table
(
EventType varchar(max),
Parameters int,
EventInfo varchar(max)
)
declare @sql table (sql varchar(max))
insert @lock
exec sys.sp_lock;
with lock
as (
select spid,LOC.type,resource,mode,status,DBS.NAME dbname,
OBJS.name objname
from @lock LOC
JOIN SYS.databases DBS
ON LOC.dbid=DBS.database_id
JOIN SYS.objects OBJS
ON LOC.objid=OBJS.object_id
where LOC.type!='DB'
)
insert @sql
select distinct 'dbcc inputbuffer ('+cast(spid as varchar)+')' as sql
from lock
DECLARE @execsql VARCHAR(max)
DECLARE pcurr CURSOR
FOR
SELECT sql
FROM @sql
OPEN pcurr
FETCH NEXT FROM pcurr INTO @execsql
WHILE (@@FETCH_STATUS=0)
BEGIN
insert @loclinfo
EXEC (
@execsql
)
----------------------------
FETCH NEXT FROM pcurr INTO @execsql
END
CLOSE pcurr
DEALLOCATE pcurr
select *
from @loclinfo