create procedure shrinkDatabase as declare @name nvarchar(2000) declare getDataBaseCursor cursor for select name from sysdatabases //取出所有库名 open getDataBaseCursor fetch next from getDataBaseCursor into @name //将取出来的值放在一个变量中 while @@fetch_status=0 //根据值循环执行压缩 begin exec ('alter database '+ @name+' set recovery simple') exec ('dbcc shrinkdatabase('+@name+')') exec ('alter database'+@name+' set recovery full') fetch next from getDataBaseCursor into @name end close getDataBaseCursor //关闭 deallocate getDataBaseCursor //释放 sp_helpdb urltest //比对数据库大小 exec shrinkDatabase //执行