Welcome to the blog site of SQLServer.in

DBCC Inputbuffer is the the coomand used to get the statement send from a client(SPID) to sqlserver.  

For instance DBCC INPUTBUFFER (81) will give the last statement from spid 81.

Here is the query to get the InputBuffer details for all SPID across all the databases.

Create proc udp_GetInputBuffer
@Dbnames varchar(1000)
As

Set NoCount On
Declare @Cnt int
Declare @GetSpid int
Declare @Dbname varchar(100)
Declare @EventType nvarchar(100)
Declare @EventInfo nvarchar(1000)

CREATE TABLE #Tempcur (
EventType NVARCHAR(100),
Parameters int,
EventInfo NVARCHAR(2000))

Create table #InputBuffer (ID int identity,Spid int,Dbname varchar(100),EventType nvarchar(100) null,
EventInfo nvarchar(1000) null,EventTime DATETIME DEFAULT CURRENT_TIMESTAMP)

Insert into #InputBuffer (Spid,Dbname)
Select spid,b.name from master.dbo.sysprocesses a
join master.dbo.sysdatabases b on a.dbid = b.dbid
WHERE CHARINDEX(‘,’ + b.name + ‘,’, @Dbnames) > 0
Set @Cnt = 1
While (Select max(ID) from #InputBuffer) >= @cnt
Begin
 Select @GetSpid = SPID,@Dbname = Dbname from #InputBuffer where ID = @Cnt

 Insert into #tempcur
 Exec(‘DBCC INPUTBUFFER(‘+@getspid+’)’)

 Select @EventType = EventType, @EventInfo = EventInfo from #tempcur

 Update #InputBuffer set EventType =@EventType,EventInfo =@EventInfo where ID = @cnt

 Truncate table #tempcur
 Set @Cnt = @cnt + 1

End
Select * from #InputBuffer order by Dbname

Drop Table #InputBuffer
Drop Table #tempcur

Set NoCount Off

2 Comments

  1. by TSwain, on February 2 2010 @ 7:26 am

     

    Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!…..I”ll be checking in on a regularly now….Keep up the good work! :)

  2. by Robert Shumake, on February 2 2010 @ 4:23 pm

     

    I usually don’t post on Blogs but ya forced me to, great info.. excellent! … I’ll add a backlink and bookmark your site.

    -Robert Shumake Paul Nicoletti

Comment RSS

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
 

About Author

Krishna is a Senior Database Administrator, having handson experience of 6 years. Areas of expertise includes database engine, Business Intelligence & performance tunning. A regular SQL blogger on his own website, msdn forums, sqlteam.com & believe in sharing his experience with other technology enthusiasts.