欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[代码实例][SQLServer]关闭连接

程序员文章站 2022-03-02 22:13:44
[代码实例][sqlserver]关闭连接。 if exists ( select * from master.sys.databases wh...

[代码实例][sqlserver]关闭连接。

if exists
(
    select * 
        from master.sys.databases
        where name='educationdb'
)
begin
    declare @spid varchar(20)
    declare curdblogin cursor for
        select cast(spid as varchar(20)) as spid
            from master.sys.sysprocesses
            where dbid=db_id('educationdb');
    open curdblogin;
    fetch next from curdblogin into @spid;
    while @@fetch_status=0
    begin
        if @spid!=@@spid
            exec('kill ' + @spid)
        fetch next from curdblogin into @spid
    end
    close curdblogin;
    deallocate curdblogin;

    drop database educationdb;
end
go