zoukankan      html  css  js  c++  java
  • Microsoft SQL Server 查询连接数和关闭连接数

    1. 查询指定数据库有哪些连接(connection)

    复制代码
    SELECT * FROM 
    [Master].[dbo].[SYSPROCESSES] WHERE [DBID] 
    IN 
    (
      SELECT 
       [DBID]
      FROM 
       [Master].[dbo].[SYSDATABASES] 
      WHERE 
       NAME='test'  ---"test"为你要查询的数据库的名字
    )
    复制代码

    2.查询整个数据库系统所有的数据库有多少连接,以分组统计的方式显示。

    复制代码
    select 
        db_name(dbid) as [Database Name], 
        count(dbid) as [No Of Connections],
        loginame as [Login Name]
    from
        sys.sysprocesses
    where 
        dbid > 0
    group by 
        dbid, loginame
    复制代码

    3.关闭指定数据库的连接

    复制代码
    set nocount on
    declare @databasename varchar(100)
    declare @query varchar(max)
    set @query = ''
    
    set @databasename = 'test' --“test”为数据库的名字
    if db_id(@databasename) < 4
    begin
        print 'system database connection cannot be killeed'
    return
    end
    
    select @query=coalesce(@query,',' )+'kill '+convert(varchar, spid)+ '; '
    from master..sysprocesses where dbid=db_id(@databasename)
    
    if len(@query) > 0
    begin
    print @query
        exec(@query)
    end
    复制代码
  • 相关阅读:
    要离职了。
    上海找工作经历
    1.6. 三基色LED
    1.5. 板载LED PWM模式
    1.4. 板载LED控制
    1.3. 硬件篇之IO口(视频连接)
    1.2 Hello World
    1.8. 数码管
    ESP32编译自己的micropython固件
    1.1 准备工作
  • 原文地址:https://www.cnblogs.com/niewa0928/p/13223166.html
Copyright © 2011-2022 走看看