DECLARE @sjk varchar(50)
SELECT @sjk=DB_NAME(dbid) FROM master.dbo.sysprocesses WHERE status='runnable'
IF @sjk='master' or @sjk='model' or @sjk='msdb' or @sjk='tempdb'
PRINT '当前数据库为系统数据库 '+@sjk+' ,不能删除其中的空表!'
ELSE
BEGIN
DECLARE tables CURSOR FOR SELECT name FROM sysobjects WHERE xtype='U'
DECLARE @t sysname
DECLARE @zbs int,@scs int
SET @zbs=0
SET @scs=0
OPEN tables
FETCH NEXT FROM tables INTO @t
WHILE @@FETCH_STATUS=0
BEGIN
SET @zbs=@zbs+1
IF (SELECT rows FROM sysindexes WHERE id=object_id(@t) and indid in(0,1))=0
BEGIN
EXEC('DROP TABLE '+@t)
SET @scs=@scs+1
END
FETCH NEXT FROM tables INTO @t
END
CLOSE tables
DEALLOCATE tables
PRINT '数据库 '+@sjk+' 中的总表数:'+CAST(@zbs as varchar(10))
PRINT '删除的空表数:'+CAST(@scs as varchar(10))
END
GO
联系客服