获取数据库当前所有活动连接的脚本

SQL2012 测试通过

获取连接

SELECT 
        [dbid],
        DB_NAME([dbid]) as DatabaseName, 
        COUNT([dbid]) as ConnectionsCount,
        [loginame] as LoginName,
        [status]
FROM
        [master].[sys].[sysprocesses]
WHERE
        [dbid] > 0
GROUP BY 
        [dbid], [loginame], [status];


获取事务

SELECT 
        [transaction_id] AS TranId,
        [name] AS TranName,
        [transaction_begin_time] AS TranStartTime,
        [transaction_state] AS TranState,
        [transaction_status] AS TranStatus
FROM 
        [master].[sys].[dm_tran_active_transactions];

正好用到上述功能,记录下。

你可能感兴趣的:(获取数据库当前所有活动连接的脚本)