SQL清除某個資料庫的所有資料庫連線

2021-05-28 01:16:50 字數 896 閱讀 2084

use master --清除連線請注意是否還use著此資料庫

go

create proc l_spcleardbconnections

@dbname varchar(30)

as--清除某個資料庫的所有資料庫連線

--rickylin 2007-11-1

declare @spid int

declare @sqlforclear nvarchar(100)

declare curid cursor forward_only read_only for (

select spid

from master.dbo.sysprocesses

where db_name(dbid) = @dbname)

open curid

fetch next from curid into @spid

while @@fetch_status = 0

begin

set @sqlforclear = n'kill ' + cast(@spid as nvarchar(10))

exec sp_executesql @sqlforclear

if @@error = 0

print '已清除連線:' + cast(@spid as varchar(10))

fetch next from curid into @spid

endclose curid

deallocate curid

print '對資料庫「' + @dbname + '」的連線清除操作完畢'

exec l_spcleardbconnections @dbname=''

清除sql表中所有資料

其實刪除資料庫中資料的方法並不複雜,為什麼我還要多此一舉呢,一是我這裡介紹的是刪除資料庫的所有資料,因為資料之間可能形成相互約束關係,刪除操作可能陷入死迴圈,二是這裡使用了微軟未正式公開的sp msforeachtable儲存過程。也許很多讀者朋友都經歷過這樣的事情 要在開發資料庫基礎上清理乙個空庫...

sql 查詢所有資料庫 表 字段

sql server 用master資料庫查詢所有的資料庫 use master select name from sysdatabases 用其中的乙個資料庫查詢所有表 use student select id name from sysobjects where type u 根據id查詢某個...

SQL 選擇資料庫以及查詢所有資料庫名 表 列名

資料庫 表 列 使用者 許可權等資訊被儲存在資料庫和表中,不過內部的表一般不直接被訪問。可用mysql的 show 命令來顯示這些資訊 mysql從內部表中提取這些資訊 sql命令 show databases 注意 該命令返回可用資料庫的乙個列表。包含在這個列表中可能是mysql內部使用的資料庫。...