How do I see active SQL Server connections?

后端 未结 8 847
暖寄归人
暖寄归人 2020-11-28 00:13

I am using SQL Server 2008 Enterprise. I want to see any active SQL Server connections, and the related information of all the connections, like from which IP address, conne

相关标签:
8条回答
  • Apart from sp_who, you can also use the "undocumented" sp_who2 system stored procedure which gives you more detailed information. See Difference between sp_who and sp_who2.

    0 讨论(0)
  • 2020-11-28 00:53
    SELECT 
        DB_NAME(dbid) as DBName, 
        COUNT(dbid) as NumberOfConnections,
        loginame as LoginName
    FROM
        sys.sysprocesses
    WHERE 
        dbid > 0
    GROUP BY 
        dbid, loginame
    ;
    

    See also the Microsoft documentation for sys.sysprocesses.

    0 讨论(0)
  • 2020-11-28 00:57

    I threw this together so that you could do some querying on the results

    Declare @dbName varchar(150)
    set @dbName = '[YOURDATABASENAME]'
    
    --Total machine connections
    --SELECT  COUNT(dbid) as TotalConnections FROM sys.sysprocesses WHERE dbid > 0
    
    --Available connections
    DECLARE @SPWHO1 TABLE (DBName VARCHAR(1000) NULL, NoOfAvailableConnections VARCHAR(1000) NULL, LoginName VARCHAR(1000) NULL)
    INSERT INTO @SPWHO1 
        SELECT db_name(dbid), count(dbid), loginame FROM sys.sysprocesses WHERE dbid > 0 GROUP BY dbid, loginame
    SELECT * FROM @SPWHO1 WHERE DBName = @dbName
    
    --Running connections
    DECLARE @SPWHO2 TABLE (SPID VARCHAR(1000), [Status] VARCHAR(1000) NULL, [Login] VARCHAR(1000) NULL, HostName VARCHAR(1000) NULL, BlkBy VARCHAR(1000) NULL, DBName VARCHAR(1000) NULL, Command VARCHAR(1000) NULL, CPUTime VARCHAR(1000) NULL, DiskIO VARCHAR(1000) NULL, LastBatch VARCHAR(1000) NULL, ProgramName VARCHAR(1000) NULL, SPID2 VARCHAR(1000) NULL, Request VARCHAR(1000) NULL)
    INSERT INTO @SPWHO2 
        EXEC sp_who2 'Active'
    SELECT * FROM @SPWHO2 WHERE DBName = @dbName
    
    0 讨论(0)
  • 2020-11-28 00:58

    You can perform the following T-SQL command:

    SELECT * FROM sys.dm_exec_sessions WHERE status = 'running';
    
    0 讨论(0)
  • 2020-11-28 01:03

    You can use the sp_who stored procedure.

    Provides information about current users, sessions, and processes in an instance of the Microsoft SQL Server Database Engine. The information can be filtered to return only those processes that are not idle, that belong to a specific user, or that belong to a specific session.

    0 讨论(0)
  • 2020-11-28 01:03

    MS's query explaining the use of the KILL command is quite useful providing connection's information:

    SELECT conn.session_id, host_name, program_name,
        nt_domain, login_name, connect_time, last_request_end_time 
    FROM sys.dm_exec_sessions AS sess
    JOIN sys.dm_exec_connections AS conn
       ON sess.session_id = conn.session_id;
    
    0 讨论(0)
提交回复
热议问题