2
我在SQL Server代理中通過sp_executesql
運行命令。如何從該命令捕獲消息,以便我可以將它們記錄在表中?檢索在sp_executesql期間打印的消息
特別是我試圖截斷數據庫日誌,並希望能夠看到這是否成功。我加入的代碼片段在代理SP:
IF (@truncate = 1)
BEGIN
SET @msg = 'Truncating log ''' + @description + ''' for the ''' + @db + ''' database'
EXEC _LogInsert 'Information', '_Loop', 'Status', @msg
--PRINT 'Attempting to truncate the log file'
SET @sql = N'USE [' + @db + ']' +
'DECLARE @LogFile varchar(128)' +
'SELECT TOP 1 @LogFile = [Name] ' +
'FROM sys.database_files ' +
'WHERE ([Type_Desc] = ''LOG'')' +
'DBCC SHRINKFILE(@LogFile, EMPTYFILE)' +
'DBCC SHRINKFILE(@LogFile, 10) WITH NO_INFOMSGS'
--PRINT 'truncation attempt finished'
EXEC sp_executesql @sql
-- Capture messages from SP
-- SET @msg = 'Truncating log msg ''' + @resmsg + ''' for the ''' + @db + ''' database'
-- EXEC _LogInsert 'Information', '_Loop', 'Status', @msg
END
您無法輕鬆捕獲所有消息。但是,您可以捕獲大多數異常,然後記錄與它們關聯的消息。 – RBarryYoung