简体   繁体   English

如何在 sql server 2005 中获取数据库连接的详细列表?

[英]How to get detailed list of connections to database in sql server 2005?

如何在 sql server 2005 中获取数据库连接的详细列表?

使用系统存储过程sp_who2

sp_who2 will actually provide a list of connections for the database server , not a database . sp_who2 实际上会提供数据库服务器的连接列表,而不是数据库 To view connections for a single database (YourDatabaseName in this example), you can use要查看单个数据库(本例中为 YourDatabaseName)的连接,您可以使用

DECLARE @AllConnections TABLE(
    SPID INT,
    Status VARCHAR(MAX),
    LOGIN VARCHAR(MAX),
    HostName VARCHAR(MAX),
    BlkBy VARCHAR(MAX),
    DBName VARCHAR(MAX),
    Command VARCHAR(MAX),
    CPUTime INT,
    DiskIO INT,
    LastBatch VARCHAR(MAX),
    ProgramName VARCHAR(MAX),
    SPID_1 INT,
    REQUESTID INT
)

INSERT INTO @AllConnections EXEC sp_who2

SELECT * FROM @AllConnections WHERE DBName = 'YourDatabaseName'

(Adapted from SQL Server: Filter output of sp_who2 .) (改编自SQL Server:筛选 sp_who2 的输出。)

As @Hutch pointed out, one of the major limitations of sp_who2 is that it does not take any parameters so you cannot sort or filter it by default.作为@Hutch指出的,主要限制之一sp_who2是,它不带任何参数,因此你无法排序或默认过滤器上。 You can save the results into a temp table , but then the you have to declare all the types ahead of time ( and remember to DROP TABLE ).您可以将结果保存到临时表中,但是您必须提前声明所有类型(并记住DROP TABLE )。

Instead, you can just go directly to the source on master.dbo.sysprocesses相反,您可以直接转到master.dbo.sysprocesses上的源

I've constructed this to output almost exactly the same thing that sp_who2 generates, except that you can easily add ORDER BY and WHERE clauses to get meaningful output.我已经构建了它以输出几乎与sp_who2生成的完全相同的内容,除了您可以轻松添加ORDER BYWHERE子句以获得有意义的输出。

SELECT  spid,
        sp.[status],
        loginame [Login],
        hostname, 
        blocked BlkBy,
        sd.name DBName, 
        cmd Command,
        cpu CPUTime,
        physical_io DiskIO,
        last_batch LastBatch,
        [program_name] ProgramName   
FROM master.dbo.sysprocesses sp 
JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid
ORDER BY spid 

There is also who is active?还有谁是活跃的? :

Who is Active?谁是活跃的? is a comprehensive server activity stored procedure based on the SQL Server 2005 and 2008 dynamic management views (DMVs).是一个基于 SQL Server 2005 和 2008 动态管理视图 (DMV) 的综合服务器活动存储过程。 Think of it as sp_who2 on a hefty dose of anabolic steroids把它想象成吃大量合成代谢类固醇的 sp_who2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM