本文来自:http://www.cnblogs.com/yjss/articles/2065664.html
1.删除表
use 数据库名 declare mycur cursor local for select [name] from dbo.sysobjects where xtype='U' declare @name varchar(100) OPEN mycur FETCH NEXT from mycur into @name WHILE @@FETCH_STATUS = 0 BEGIN exec('drop table ' + @name) FETCH NEXT from mycur into @name END CLOSE mycur
2.删除视图
use 数据库名 declare mycur cursor local for select [name] from dbo.sysobjects where xtype='V' declare @name varchar(100) OPEN mycur FETCH NEXT from mycur into @name WHILE @@FETCH_STATUS = 0 BEGIN exec('drop VIEW ' + @name) FETCH NEXT from mycur into @name END CLOSE mycur
3.删除存储过程
use 数据库名 declare mycur cursor local for select [name] from dbo.sysobjects where xtype='P' declare @name varchar(100) OPEN mycur FETCH NEXT from mycur into @name WHILE @@FETCH_STATUS = 0 BEGIN exec('drop PROCEDURE ' + @name) FETCH NEXT from mycur into @name END CLOSE mycur
4.查看被锁表
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT'
5.清除锁死的表
Exec dbo.sp_lock begin declare @i int--定义要清除的线程id declare @SQL nvarchar(3000); set @i =0; while(@i<10000) begin --清除所有的占用线程 set @SQL=N'kill '+convert(varchar(20),@i) exec sp_executesql @SQL; set @i=@i+1; end end