Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Shrink logs
Message
 
 
À
10/07/2009 15:16:29
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01411487
Message ID:
01411491
Vues:
37
Just use sp_MSForEachDB procedure for your loop.

Also see http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/automatically-create-a-sql-server-table-

(expands Sergey's idea)

>There is probably a much better way to do this, but this is not working anyway.
>
>ShairPoint (MOSS) creates its own DBs. Evidently it can not be told to create them in SIMPLE recovery mode, and in this case, 100s of DBs have been created. I am trying to change the recovery mode to SIMPLE and shrink the log file for every DB (not system DBs) in a script. In the script (below) I try to change the DB focus using the USE command, but evidently it is not working as I thought, because I get errors that say the log file can not be found in the master.
>
>"Could not locate file 'ReportServer_log' for database 'master' in sys.database_files."
>
>
>/*
>This script chages the DB recevery type to SIMPLE, and 
>can change it back to FULL if the lines are not commented out.
>In the mean time, it shrinks the log file size, which is
>the goal of this script.
>
>Written by Rollin Burr
>*/
>
>SET NOCOUNT ON
>
>DECLARE @DBName VARCHAR (128),
>	@LogFile	VARCHAR (255),
>	@ExeString	VARCHAR(512)
>
>DECLARE cDBs CURSOR FOR 
>SELECT [name]
>FROM master.dbo.sysdatabases
>WHERE [name] NOT IN ('master', 'tempdb', 'model', 'msdb')
>OPEN cDBs
>FETCH NEXT FROM cDBs INTO @DBName
>WHILE @@fetch_status = 0
>BEGIN
>	SET @ExeString = 'USE ' + QUOTENAME(@DBName) + ';'
>	EXEC (@ExeString)
>
>	SET @LogFile = (
>		SELECT     dbo.sysaltfiles.name
>		FROM       master.dbo.sysdatabases INNER JOIN dbo.sysaltfiles 
>					ON master.dbo.sysdatabases.dbid = dbo.sysaltfiles.dbid
>		WHERE     (master.dbo.sysaltfiles.fileid = 1) AND (master.dbo.sysdatabases.name = @DBName)
>	)
>
>	SET @ExeString = 'USE ' + QUOTENAME(@DBName) + ';'
>	EXEC (@ExeString)
>
>	-- Truncate the log by changing the database recovery model to SIMPLE.
>	SET @ExeString = 'ALTER DATABASE ' + QUOTENAME(@DBName) + 'SET RECOVERY SIMPLE'
>	EXEC (@ExeString)
>
>	-- Shrink the truncated log file to 1 MB.
>	SET @ExeString = 'DBCC SHRINKFILE (' + QUOTENAME(@LogFile) + ', 1)'
>	EXEC (@ExeString)
>
>	-- Comment this in if you want to return the DBs to FULL.
>	--SET @ExeString = 'ALTER DATABASE ' + QUOTENAME(@DBName) + 'SET RECOVERY FULL'
>	--EXEC (@ExeString)
>
>	FETCH NEXT FROM cDBs INTO @DBName
>END
>CLOSE cDBs 
>DEALLOCATE cDBs
>
>
>
>Any help is always appreciated.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform