CREATE PROCEDURE dbo.spGET_LastErrorMessage AS BEGIN DECLARE @dbccrow nchar(77) ,@sql nvarchar(2000) ,@hex nchar(2) ,@byte int ,@pos int ,@numMsg int ,@gather int ,@count int ,@byteNum int ,@msgLen int ,@errMsgLen int ,@nchar int ,@errNumber bigint ,@errState int ,@errLevel int ,@errMessage nvarchar(1000) ,@errInstance nvarchar(256) ,@errProcedure nvarchar(256) ,@errLine int
CREATE TABLE #DBCCOUT (col1 nchar(77)) INSERT INTO #DBCCOUT EXEC ('DBCC OUTPUTBUFFER(@@spid)')
CREATE TABLE #errors ( errNumber bigint ,errState int ,errLevel int ,errMessage nvarchar(1000) ,errInstance nvarchar(256) ,errProcedure nvarchar(256) ,errLine int )
DECLARE error_cursor CURSOR STATIC FORWARD_ONLY FOR SELECT col1 FROM #DBCCOUT ORDER BY col1
OPEN error_cursor FETCH NEXT FROM error_cursor INTO @dbccrow
SET @numMsg = 0
SET @pos = 12 SET @gather = 0
WHILE (@@FETCH_STATUS = 0) BEGIN Start: IF (@pos > 57) BEGIN SET @pos = 12 GOTO NextRow END
SET @hex = Substring(@dbccrow, @pos, 2)
SELECT @sql = 'SELECT @int = convert(int, 0x00' + @hex + ')' EXEC sp_executesql @sql, N'@int int OUTPUT', @byte output SET @pos = @pos + 3
IF (@gather = 0) BEGIN IF (@byte != 170) GOTO Start
SET @gather = 1 SET @count = 0 SET @msgLen = 0 GOTO Start END
IF (@gather = 1) BEGIN SET @count = @count + 1 SET @msgLen = (@msgLen * 256) + @byte
IF (@count = 2) BEGIN SET @count = 0 SET @byteNum = 0 SET @errNumber = 0 SET @gather = 2 END
GOTO Start END
IF (@gather > 1) SET @byteNum = @byteNum + 1
IF (@gather = 2) BEGIN SET @errNumber = IsNull(@errNumber, 0) + (@byte * power(256, @count)) SET @count = @count + 1 IF (@count = 4) BEGIN SET @count = 0 SET @gather = 3 END
GOTO Start END
IF (@gather = 3) BEGIN SET @gather = 4 SET @errState = @byte
GOTO Start END
IF (@gather = 4) BEGIN SET @gather = 5 SET @errLevel = @byte
GOTO Start END
IF (@gather = 5) BEGIN SET @errMsgLen = IsNull(@errMsgLen, 0) + (@byte * Power(256, @count)) SET @count = @count + 1 IF (@count = 2) BEGIN SET @nchar = 0 SET @count = 0 SET @gather = 6 END
GOTO Start END
IF (@gather = 6) BEGIN IF (@errMsgLen > 0) BEGIN SET @nchar = IsNull(@nchar, 0) + (@byte * Power(256, @count)) SET @count = @count + 1 IF (@count = 2) BEGIN SET @count = 0 SET @errMessage = IsNull(@errMessage, '') + nchar(@nchar) SET @nchar = 0 END
IF (Len(@errMessage) = @errMsgLen) SET @gather = 7
GOTO Start END ELSE SET @gather = 7 END
IF (@gather = 7) BEGIN SELECT @gather = 8 ,@errMsgLen = @byte ,@nchar = 0
Goto Start END
IF (@gather = 8) BEGIN IF (@errMsgLen > 0) BEGIN SET @nchar = IsNull(@nchar, 0) + (@byte * Power(256, @count)) SET @count = @count + 1 IF (@count = 2) BEGIN SET @count = 0 SET @errInstance = IsNull(@errInstance, '') + nchar(@nchar) SET @nchar = 0 END
IF (Len(@errInstance) = @errMsgLen) SET @gather = 9
GOTO Start END ELSE SET @gather = 9 END
IF (@gather = 9) BEGIN SELECT @gather = 10 ,@errMsgLen = @byte ,@nchar = 0
Goto Start END
IF (@gather = 10) BEGIN IF (@errMsgLen > 0) BEGIN SET @nchar = IsNull(@nchar, 0) + (@byte * Power(256, @count)) SET @count = @count + 1 IF (@count = 2) BEGIN SET @count = 0 SET @errProcedure = IsNull(@errProcedure, '') + nchar(@nchar) SET @nchar = 0 END
IF (Len(@errProcedure) = @errMsgLen) SET @gather = 11
GOTO Start END ELSE SET @gather = 11 END
IF (@gather = 11) BEGIN SET @errLine = IsNull(@errLine, 0) + (@byte * Power(256, @count)) SET @count = @count + 1 IF (@count = 2) BEGIN SET @nchar = 0 SET @count = 0 SET @gather = 0 SET @nchar = 0
INSERT #errors VALUES (@errNumber, @errState, @errLevel, @errMessage, @errInstance, @errProcedure, @errLine) END
GOTO Start END
NextRow: FETCH NEXT FROM error_cursor INTO @dbccrow END
CLOSE error_cursor DEALLOCATE error_cursor
SELECT * FROM #errors END GO
raiserror ('TEST 1', 16 ,1) GO exec spGET_LastErrorMessage |