For each database – sp_MSForEachDB

sp_helptext 'sp_msforeachdb'


declare @ssql nvarchar(4000)
set @ssql= '
if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') begin
use [?]
declare @tsql nvarchar(4000) set @tsql = ''''
declare @iLogFile int
declare @sLogFileName varchar(55)
declare @RecoveryModel nvarchar(10)
declare LogFiles cursor for
select fileid from sysfiles where status & 0x40 = 0x40
open LogFiles
fetch next from LogFiles into @iLogFile
while @@fetch_status = 0
begin
set @tsql = @tsql + ''DBCC SHRINKFILE(''+cast(@iLogFile as varchar(5))+'', 10) ''
fetch next from LogFiles into @iLogFile
end
select @RecoveryModel = CONVERT(nvarchar(10),Databasepropertyex(name, ''Recovery'')) from master..sysdatabases where name = ''?''
set @tsql = ''USE [?]; '' + @tsql + '' ALTER DATABASE [?] SET RECOVERY SIMPLE '' + @tsql
set @tsql = @tsql + ''; ALTER DATABASE [?] SET RECOVERY '' + @RecoveryModel + '';''
--print @tsql --for debugging
exec(@tsql)
close LogFiles
DEALLOCATE LogFiles
end'

exec sp_msforeachdb @ssql



http://www.sqllion.com/2010/08/for-each-database-sp_msforeachdb/

http://codesnippets.joyent.com/posts/show/665

你可能感兴趣的:(database)