/*
修改所有用户数据库的恢复模式为简单模式
*/
EXEC sp_msforeachdb ‘
DECLARE @dbname VARCHAR(30)
SET @dbname=‘‘?‘‘
DECLARE @exsql VARCHAR(500)
IF(@dbname not in (
SELECT name FROM sys.databases WHERE database_id<=4 -- 非系统表
or recovery_model_desc=‘‘SIMPLE‘‘ -- 非简单恢复模式
or name=‘‘ReportServer‘‘ or name=‘‘ReportServerTempDB‘‘-- 非报表库
OR is_read_only=1 -- 非只读
OR state=6 -- 非脱机
or is_distributor=1 -- 非订阅库
or is_published=1 -- 非发布库
or is_subscribed=1 -- 非分发库
UNION
SELECT DB_NAME(database_id) FROM sys.database_mirroring WHERE mirroring_role IS NOT NULL -- 非镜像库
))
BEGIN
SELECT @dbname=name FROM master.sys.databases WITH ( NOLOCK ) WHERE [email protected]
SET @exsql =‘‘use master alter database ‘‘[email protected]+‘‘ set recovery simple with no_wait‘‘
begin try
EXEC(@exsql)
print @dbname
end try
begin catch
select @dbname+ERROR_MESSAGE()
end catch
END
‘
/*
用于数据库还原后,修改所有数据库的所有者为sa(防止ssb不通)
*/
EXEC sp_MSforeachdb ‘
DECLARE @dbname NVARCHAR(100);
DECLARE @sql NVARCHAR(max);
SELECT @dbname = ‘‘?‘‘;
if (@dbname not in (
SELECT name FROM sys.databases WHERE database_id<=4 -- 非系统表
--or recovery_model_desc=‘‘SIMPLE‘‘ -- 非简单恢复模式
or name=‘‘ReportServer‘‘ or name=‘‘ReportServerTempDB‘‘-- 非报表库
OR is_read_only=1 -- 非只读
OR state=6 -- 非脱机
or is_distributor=1 -- 非订阅库
or is_published=1 -- 非发布库
or is_subscribed=1 -- 非分发库
UNION
SELECT DB_NAME(database_id) FROM sys.database_mirroring WHERE mirroring_role IS NOT NULL -- 非镜像库
))
begin
SELECT @dbname=name FROM master.sys.databases WITH ( NOLOCK ) WHERE [email protected]
set @sql = ‘‘use ‘‘ + @dbname +
‘‘ exec sp_changedbowner ‘‘‘‘sa‘‘‘‘ ‘‘
begin try execute (@sql) print @dbname end try
begin catch select @dbname+ERROR_MESSAGE() end catch
end
‘