Sql-Server-2014
如何從 sp_MSforeachdb 中排除 tempdb?
下面我有一個程式碼來獲取有關伺服器中每個數據庫的 t-log 備份的資訊。我想從這個列表中排除 tempdb。
exec sp_MSforeachdb 'SELECT server_name, sysdb.name AS DatabaseName, bkup.user_name AS [User], ceiling(bkup.backup_size /1048576) as ''Size Meg'' , cast((bkup.backup_size /1073741824) as decimal (9,2)) as ''Gig'', bkup.backup_start_date AS [Backup Started], bkup.backup_finish_date AS [Backup Finished (Last BackUp Time)], CAST((CAST(DATEDIFF(s, bkup.backup_start_date, bkup.backup_finish_date) AS int))/3600 AS varchar) + '' hours, '' + CAST(DATEDIFF(mi, bkup.backup_start_date, bkup.backup_finish_date) - (DATEDIFF(mi, bkup.backup_start_date, bkup.backup_finish_date)/60)*60 AS varchar) + '' minutes, '' + CAST((CAST(DATEDIFF(s, bkup.backup_start_date, bkup.backup_finish_date) AS int))%60 AS varchar)+ '' seconds'' AS [Total Time] ,DATEDIFF(DAY,CONVERT(CHAR(8),backup_finish_date,112),CONVERT(CHAR(8),expiration_date,112)) AS expiration_days ,bms.description AS [Description] ,bkup.is_damaged AS isDamaged ,bms.is_compressed AS isCompressed ,bkup.is_copy_only AS isCopyOnly ,bkup.database_creation_date AS DatabaseCreationDate ,bmf.physical_device_name AS PhysicalDeviceName ,CASE WHEN (bkup.backup_start_date is NULL OR bkup.backup_start_date < DATEADD(dd,-1,GetDate()) ) THEN ''Yes'' ELSE ''No'' END AS isOlderThan24Hours ,bkup.database_backup_lsn ,bkup.differential_base_lsn ,bkup.first_lsn ,bkup.last_lsn ,bkup.fork_point_lsn FROM master.dbo.sysdatabases sysdb LEFT OUTER JOIN msdb.dbo.backupset bkup ON bkup.database_name = sysdb.name INNER JOIN msdb.dbo.backupmediafamily AS bmf ON bkup.media_set_id = bmf.media_set_id LEFT outer JOIN sys.backup_devices AS bd ON bmf.device_type = bd.type LEFT outer JOIN msdb.dbo.backupmediaset AS bms ON bkup.media_set_id = bms.media_set_id --WHERE backup_finish_date = (SELECT MAX(bkup.backup_finish_date) FROM msdb.dbo.backupset bkup WHERE sysdb.name = bkup.database_name) --Last backup WHERE backup_finish_date > DATEADD(DAY, -1, (getdate())) -- Last 60 days AND bkup.type=''L'' AND sysdb.name = ''?'' ORDER BY backup_start_date DESC, backup_finish_date';
我試著寫 ‘IF ‘’?’’ NOT IN (‘’tempdb’’) SELECT…….’ 但只有一個數據庫的所有結果都是相同的。然後我嘗試編寫 AND sysdb.name <> ‘’tempdb’’’’ 但它給出了一個錯誤:
Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'. Msg 102, Level 15, State 1, Line 32 Incorrect syntax near 'DES'.
一種可能是檢查 DB_Name = ‘Tempdb’,如果是 tempdb,則結束腳本。
這是我做的一個快速測試,它似乎有效:
EXEC sp_msforeachdb 'USE [?] IF DB_Name() = ''Tempdb''BEGIN RETURN END SELECT db_Name()'
這將在每個數據庫上執行程式碼,但如果它是 tempdb,它不會執行任何事情,因為它會返回。
眾所周知,使用 sp_msforeachdb 並不總是最好的。亞倫在部落格上寫了一篇關於使用你可以使用的自製更換程序的部落格,可能值得你花時間去看看!製作更可靠、更靈活的 spmsforeachdb