如何实时查看数据库当前活动连接

原来一直被这个问题所困扰,前些天在网上偶然发现解决的办法。现分享如下:
基本语句为 sp_who
我们可以把查询结果插入到我们自己定义的表(MonitorUser)中
基本语句为
CREATE table MonitorUser
( spid int,
ecid int,
status varchar(200),
loginame varchar(200),
hostname varchar(200), \\根据sp_who的结果新建表结构
blk varchar(200),
dbname varchar(200),
cmd varchar(200),
[DateTime] [datetime] NOT NULL
)
ALTER TABLE [dbo].[MonitorUser] WITH NOCHECK ADD
CONSTRAINT [DateTime] DEFAULT (getdate()) for[DateTime] \\自动获取时间插到datetime列中
GO
insert into MonitorUser(spid,ecid,status,loginame,hostname,blk,dbname,cmd)
exec ('sp_who') \\执行sp_who,将结果插入到monitorUser中

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus