本文介紹一個簡單的SQL腳本,實現收縮整個Microsoft SQL Server實例所有非系統DB文件大小的功能。 作為一個與SQL天天打交道的程式猿,經常會遇到DB文件太大,把空間占滿的情況: 而對於開發測試人員來說,如果DB數據不是特別重要的話,不會特意擴大磁碟空間,而是直接利用SQL的Shr ...
本文介紹一個簡單的SQL腳本,實現收縮整個Microsoft SQL Server實例所有非系統DB文件大小的功能。
作為一個與SQL天天打交道的程式猿,經常會遇到DB文件太大,把空間占滿的情況:
而對於開發測試人員來說,如果DB數據不是特別重要的話,不會特意擴大磁碟空間,而是直接利用SQL的Shrink File功能縮小DB文件大小,詳見:https://docs.microsoft.com/en-us/sql/relational-databases/databases/shrink-a-file。
這裡介紹一個腳本,支持一鍵收縮整個SQL Server實例上所有非系統DB文件大小。
腳本支持功能和相關邏輯如下:
- 五個系統DB(master、model、msdb、tempdb、Resource)不會執行Shrink File操作;
- 如果DB的Recovery Model是FULL或者BULK_LOGGED,會自動改成SIMPLE,Shrink File操作後再改回原來的;
- 只會對DB狀態為ONLINE的DB進行Shrink File操作;
- DB的所有文件,包括數據文件和日誌文件都會被執行收縮操作;
- 執行SQL腳本的用戶需要有sysadmin或者相關資料庫的DBO許可權。
腳本如下:
1 -- Created by Bob from http://www.cnblogs.com/lavender000/ 2 use master 3 DECLARE dbCursor CURSOR for select name from [master].[sys].[databases] where state = 0 and is_in_standby = 0; 4 DECLARE @dbname NVARCHAR(255) 5 DECLARE @recoveryModel NVARCHAR(255) 6 DECLARE @tempTSQL NVARCHAR(255) 7 DECLARE @dbFilesCursor CURSOR 8 DECLARE @dbFile NVARCHAR(255) 9 DECLARE @flag BIT 10 11 OPEN dbCursor 12 FETCH NEXT FROM dbCursor INTO @dbname 13 14 WHILE @@FETCH_STATUS = 0 15 BEGIN 16 if((@dbname <> 'master') and (@dbname <> 'model') and (@dbname <> 'msdb') and (@dbname <> 'tempdb') and (@dbname <> 'Resource')) 17 begin 18 print('') 19 print('Database [' + @dbname + '] will be shrinked log...') 20 SET @flag = 1 21 SET @recoveryModel = (SELECT recovery_model_desc FROM sys.databases WHERE name = @dbname) 22 if((@recoveryModel = 'FULL') or (@recoveryModel = 'BULK_LOGGED')) 23 begin 24 SET @tempTSQL = (select CONCAT('ALTER DATABASE [', @dbname, '] SET RECOVERY SIMPLE with no_wait')) 25 EXEC sp_executesql @tempTSQL 26 if (@@ERROR = 0) 27 begin 28 print(' Database [' + @dbname + '] recovery model has been changed to ''SIMPLE''.') 29 SET @flag = 1 30 end 31 else 32 begin 33 print('Database [' + @dbname + '] recovery model failed to be changed to ''SIMPLE''.') 34 SET @flag = 0 35 end 36 end 37 38 if(@flag = 1) 39 begin 40 SET @tempTSQL = (select CONCAT('use [', @dbname, ']')) 41 EXEC sp_executesql @tempTSQL 42 SET @dbFilesCursor = CURSOR for select sys.master_files.name from sys.master_files, [master].[sys].[databases] where databases.name = @dbname and databases.database_id = sys.master_files.database_id 43 open @dbFilesCursor 44 FETCH NEXT FROM @dbFilesCursor INTO @dbFile 45 WHILE @@FETCH_STATUS = 0 46 BEGIN 47 SET @tempTSQL = (select CONCAT('use [', @dbname, '] DBCC SHRINKFILE (N''', @dbFile, ''') with NO_INFOMSGS')) 48 EXEC sp_executesql @tempTSQL 49 if(@@ERROR = 0) print(' Database file [' + @dbFile + '] has been shrinked log successfully.') 50 FETCH NEXT FROM @dbFilesCursor INTO @dbFile 51 END 52 CLOSE @dbFilesCursor 53 DEALLOCATE @dbFilesCursor 54 55 if(@recoveryModel <> 'SIMPLE') 56 begin 57 -- Finally changed back 58 SET @tempTSQL = (select CONCAT('ALTER DATABASE [', @dbname, '] SET RECOVERY ', @recoveryModel, ' with no_wait')) 59 EXEC sp_executesql @tempTSQL 60 if (@@ERROR = 0) 61 begin 62 print(' Database [' + @dbname + '] recovery model has been changed back to ''' + @recoveryModel + '''') 63 end 64 else 65 begin 66 print(' Database [' + @dbname + '] recovery model failed to be changed back to ''' + @recoveryModel + '''') 67 end 68 end 69 end 70 end 71 FETCH NEXT FROM dbCursor INTO @dbname 72 END 73 74 CLOSE dbCursor 75 DEALLOCATE dbCursor
執行完效果如下:
Note:
- 如果不放心使用,可提前備份相關資料庫;
- 使用前請仔細閱讀腳本支持功能和相關邏輯,如與自己需求不符,請不要使用該腳本,或者請根據自己需求自行修改腳本;
- 腳本為簡易腳本,僅用於測試學習,可能有BUG,不可生產環境使用,如有錯誤,請留言。
[原創文章,轉載請註明出處,僅供學習研究之用,如有錯誤請留言,謝謝支持]
[原站點:http://www.cnblogs.com/lavender000/p/6882741.html,來自永遠薰薰]