我尝试自动化释放SQL服务器中数据库空间的过程,为此我组装了以下脚本和3个特定情况(根据机构的业务规则)。第一个是tempdb
,我将此数据库分开,因为我无法应用alter ... recovery
(我不知道是否有必要将空间释放应用于此数据库)。第二个规则是针对特定数据库BD_MAIN
,它有四个数据文件和四个日志文件,我只申请最后两个文件。第三种情况是通过丢弃所有剩余的数据库。
EXEC sp_MSforeachdb
'
use [?]
declare
@arch_1 sysname,
@arch_2 sysname,
@bd_name sysname
set @bd_name=(select DB_NAME())
set @arch_1 =(select name FROM SYSFILES
where fileid = ''1'')
set @arch_2 =(select name FROM SYSFILES
where fileid = ''2'')
-- 1st case: If the data base is tempdb:
if (@bd_name=''tempdb'')
begin
select name from sysfiles
DBCC SHRINKFILE (tempdev, 1)
DBCC SHRINKFILE (templog, 1)
end
-- 2nd case: If the data base is BD_MAIN:
else
begin
if (@bd_name=''BD_MAIN'')
begin
select name from sysfiles
DBCC SHRINKFILE (BD_MAIN4 , 1);
ALTER DATABASE BD_MAIN
SET RECOVERY SIMPLE
DBCC SHRINKFILE (BD_MAIN_log4 , 1);
ALTER DATABASE BD_MAIN
SET RECOVERY FULL
end
-- 3rd case: Another data bases:
else
begin
select name from sysfiles
DBCC SHRINKFILE (@arch_1 , 1)
ALTER DATABASE [?]
SET RECOVERY SIMPLE
DBCC SHRINKFILE (@arch_2 , 1)
ALTER DATABASE [?]
SET RECOVERY FULL
end
end
'
我对这个脚本的问题是当我在'tempdb'时,我认为我不应该输入最后else
(对于其他情况),但它会抛出以下错误消息(第4行):
DBCC execution completed. If there are error messages, consult your system administrator.
DBCC execution completed. If there are error messages, consult your system administrator.
Mens. 5058, Level 16, State 1, Line 44
The 'RECOVERY' option can not be set in the 'tempdb' database.
DBCC execution completed. If there are error messages, consult your system administrator.
You can not reduce the file '2' in the database 'model' to 128 pages, since it only contains 96 pages.
DBCC execution completed. If there are error messages, consult your system administrator.
DBCC execution completed. If there are error messages, consult your system administrator.
DBCC execution completed. If there are error messages, consult your system administrator.
DBCC execution completed. If there are error messages, consult your system administrator.
我想知道是否可以改进代码以避免这种错误消息,因为我想将它合并到存储过程或关于空间释放的一些建议(如何,何时以及何时进行)。