MSSQL查询当前登录进程以及执行状态


--当前连接进程
declare @tempTable table (SPID INT,Status VARCHAR(255),
Login VARCHAR(255),HostName VARCHAR(255),
BlkBy VARCHAR(255),DBName VARCHAR(255),
Command VARCHAR(255),CPUTime INT,
DiskIO INT,LastBatch VARCHAR(255),
ProgramName VARCHAR(255),SPID2 INT,
REQUESTID INT);

INSERT INTO @tempTable
EXEC sp_who2

select * from @tempTable where DBName in ('SpaceBuilder5',
'WindinFundDB_3',
'WindinHome_DB',
'WindinLog_DB',
'WindinStockDB_2',
'wwms_db')


--查看执行SQL
SELECT DISTINCT
[Spid] = STR(a.spid, 4)
, [Status] = CONVERT(CHAR(10), a.status)
, [LockProcID] = STR(a.blocked, 2)
, [HostName] = CONVERT(CHAR(10), a.hostname)
, [UserName] = CONVERT(CHAR(10), SUSER_NAME(a.uid))
, [DB_Name] = CONVERT(CHAR(10), DB_NAME(a.dbid))
, [program_name] = CONVERT(CHAR(10), a.program_name)
, [Command] = CONVERT(CHAR(16), a.cmd)
, [LoginName] = a.loginame
, [TEXT] = b.text
FROM master..sysprocesses a CROSS APPLY
sys.dm_exec_sql_text(a.sql_handle) b
WHERE a.blocked IN ( SELECT blocked
FROM master..sysprocesses )

posted @ 2019-12-21 20:19  kelelipeng  阅读(664)  评论(0编辑  收藏  举报