Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error Handling

Author  Topic 

SQLNOVICE999
Yak Posting Veteran

62 Posts

Posted - 2012-02-07 : 15:14:52
Hi Guys,

I created a wrapper SP that will execute two SPs...Since this will be in SQL 2000, I wanted to log the errors into table and couldn't use Try Catch....

The Proc should fail and log into the ERROR_LOG...

For testing purposes, I mis-spelt one of the procs executed by the sp_test_error... I am doing something wrong as the Proc is erroring out, but not logging error into ERROR_LOG using sql in PROBLEM:

What I need to do is basically log errors into error table.... If it is possible, I would like to execute the proc that does not error out....

Thanks in advance.

Laura


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_test_error]

AS
BEGIN

DECLARE @intErrorCode INT
DECLARE @source varchar(50)


BEGIN TRAN
SET @source = 'sp_someProc1'
EXEC dbo.sp_someProc1

SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM

SET @source = 'sp_someProc2'
EXEC dbo.sp_someProc2

SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM

COMMIT TRAN

Return 0

PROBLEM:

Insert into ERROR_LOG (ERROR_DATE, ERROR_USER, [ERROR_MESSAGE], ERROR_SOURCE, [ERROR_NUMBER], ERROR_HOST)
Select getdate(), SYSTEM_USER, '',@source, @intErrorCode, HOST_NAME()

ROLLBACK TRAN

Return 1

END

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-07 : 15:18:03
see

http://www.sommarskog.se/error-handling-I.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

62 Posts

Posted - 2012-02-07 : 15:33:23
Probably I should add error handling in each SP and not worry about it here...
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 15:55:02
You need to do you error logging OUTSIDE of the transaction

When you do a rollback AFTER your log insert, that too will be rolled back

I do Sproc Logging for all my Sprocs...and I try to limit these "wrappers" as much as possible..nested transactions and such

Create one common logging Sproc that all sprocs will use, and pass in parameters. We even set a Parameter Column to mimic the EXACT Call that was made for troubleshooting...because developers lie...or are incompetent

CREATE PROC mySproc99
@vars....
AS

DECLARE @s datetime, @e datetime, @Error_Type int, @Error_Loc int, @sprc int, @rowcount2 int, @Parameters varchar(MAX), @Sprocname varchar(255)

SELECT @rc = 0, @s = GetDate(), @Error = 0, @Error_Message='', @Rowcount = 0, @Rowcount2 = 0, @Sprocname = object_name(@@procid)

BEGIN TRAN

SQL

SELECT @Error = @@ERROR, @Rowcount = @@ROWCOUNT

IF @Error <> 0
BEGIN
Stuff
GOTO sp_Error
END

IF @Rowcount = 0 -- Lets assume you expect a row modification
BEGIN
Stuff
GOTO sp_Error
END

COMMIT TRAN

sp_Exit:

..housekeeping

SET @e = GetDate()
SET @Parameters = 'DECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int; '+CHAR(13)+CHAR(10)
+ 'EXEC ' + @SprocName +CHAR(13)+CHAR(10)
+ ' @Category_Cd=' +COALESCE( RTRIM(@Category_Cd) ,'NULL')+CHAR(13)+CHAR(10)
+ ', @User=' +COALESCE(CHAR(39)+RTRIM(@User) +CHAR(39),'NULL')+CHAR(13)+CHAR(10)
+ ', @rc=@rc OUTPUT, @Error=@Error OUTPUT, @Error_Message=@Error_Message OUTPUT, @Rowcount=@Rowcount OUTPUT; ' +CHAR(13)+CHAR(10)
+ 'SELECT @rc AS [RC], @Error AS [Error], @Error_Message AS [Error_Message], @Rowcount AS [RowCount]'
EXEC [isp_LOG_SprocExecLog] @Sprocname, @s, @e, @rc, @Error, @Error_Message, @Rowcount, @User, @Parameters, @sprc OUTPUT
SET NOCOUNT OFF
RETURN

sp_Error:

ROLLBACK TRAN
RAISE_ERROR

GOTO sp_Exit






Here's our logging sproc


USE [myActions]
GO
/****** Object: StoredProcedure [dbo].[isp_LOG_SprocExecLog] Script Date: 04/30/2010 14:37:34 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[isp_LOG_SprocExecLog]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[isp_LOG_SprocExecLog]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO


CREATE PROCEDURE [dbo].[isp_LOG_SprocExecLog]
@SprocName nvarchar(256)
,@TranStart datetime
,@TranEnd datetime
,@ReturnCode int
,@ErrorCode int
,@ErrorMessage nvarchar(4000) = null
,@Rows int
,@App_User nchar(30)
,@Parameters varchar(max)
,@RC int OUTPUT
AS


--
-- ATE Technology Services
--
-- File: \\paerscns20\ss\GBTS_Systems_Projects\Projects\myActions\Database\SQLServer\NJROS1BBLD0304\DEV2K08\Sprocs
-- Date: 2011-07-08
-- Author: Brett Kaiser
-- Server: paerscbvd0403\dev
-- Database: myActions
-- Login: sa
-- Description: This Procedure will log all procedures executed in a database.
--
--
-- The stream will do the following:
--
-- '1. Function...
--
--
-- Tables Used: [SprocExecLog]
--
-- Tables Created: None
--
--
-- Row Estimates:
-- name rows reserved data index_size unused
-- --------------------------------------------------------------------------------------------------
-- [SprocExecLog] 0 0 KB 0 KB 0 KB 0 KB
--
-- sp_spaceused [SprocExecLog]
--
--Change Log
--
-- UserId Date Description
-- ----------- -------------- ------------------------------------------------------------------------------------------
-- x002548 2011/07/08 1. Initial release

/*
Sample Execution:


DECLARE @RC int
EXEC [dbo].[isp_LOG_SprocExecLog]
'TestSproc' --@SprocName nvarchar(256)
, '2010-04-16 13:19:39.620' --@TranStart datetime
, '2010-04-16 13:19:39.620' --@TranEnd datetime
, 99 --@ReturnCode int
, 99 --@ErrorCode int
, 'No error, just a test.' --@ErrorMessage nvarchar(4000)
, 99 --@Rows int
, 'X036992' --@App_User nchar(7)
, 'Test SprocLog Execution Log Load' --@Parameters varchar(max)
, @RC OUTPUT --@rc int OUTPUT
SELECT @RC AS [RC]

SELECT TOP 10 * FROM [SprocExecLog]
--WHERE SprocName = 'TestSproc'
ORDER BY TranStart DESC

*/

DECLARE @RowCount int, @Error_Loc int, @Error_Type int, @Error_Message varchar(255), @Error int
DECLARE @s datetime, @Log_ID int
SELECT @rc = 0, @s = GETDATE()

BEGIN TRAN

INSERT INTO [dbo].[SprocExecLog] (
[SprocName]
,[TranStart]
,[TranEnd]
,[LogStart]
,[LogEnd]
,[ReturnCode]
,[ErrorCode]
,[ErrorMessage]
,[Rows]
,[App_User]
,[Parameters]
)
SELECT
@SprocName
,@TranStart
,@TranEnd
,@s
,GetDate()
,@ReturnCode
,@ErrorCode
,@ErrorMessage
,@Rows
,@App_User
,@Parameters

SELECT @ErrorCode = @@ERROR, @Rowcount = @@ROWCOUNT, @Log_Id = SCOPE_IDENTITY()

IF @ErrorCode <> 0
BEGIN
SELECT @Error_Loc = 2, @Error_Type = 50001, @rc = -6662
GOTO isp_LOG_SprocExecLog_Error
END

IF @RowCount <> 1
BEGIN
SELECT @Error_Loc = 3
, @Error_Message = 'Expected 1 row to be inserted in to the SprocExecLog. Actual Number inserted = '
+ CONVERT(varchar(5),@RowCount)
, @Error_Type = 50002, @rc = -6663
GOTO isp_LOG_SprocExecLog_Error
END


COMMIT TRAN


isp_LOG_SprocExecLog_Exit:

--UPDATE [SprocExecLog] SET [LogEnd] = GetDate()
--WHERE [Log_ID] = @Log_ID

SET NOCOUNT OFF
RETURN

isp_LOG_SprocExecLog_Error:
ROLLBACK TRAN

IF @Error_Type = 50001
BEGIN
SELECT @error_message = (SELECT 'Location: ' + ',"' + RTRIM(CONVERT(char(3),@Error_Loc))
+ ',"' + ' @@ERROR: ' + ',"' + RTRIM(CONVERT(char(6),error))
+ ',"' + ' Severity: ' + ',"' + RTRIM(CONVERT(char(3),severity))
-- + ',"' + ' Message: ' + ',"' + RTRIM(description)
FROM master..sysmessages
WHERE error = @error)
END

IF @Error_Type = 50002
BEGIN
SELECT @Error_Message = 'Location: ' + ',"' + RTRIM(CONVERT(char(3),@Error_Loc))
+ ',"' + ' Severity: UserLevel '
+ ',"' + ' Message: ' + ',"' + RTRIM(@Error_Message)
END

RAISERROR @Error_Type @Error_Message
GOTO isp_LOG_SprocExecLog_Exit
GO

GRANT VIEW DEFINITION ON [dbo].[isp_LOG_SprocExecLog] TO [myActions_User99_Role]
GRANT EXECUTE ON [dbo].[isp_LOG_SprocExecLog] TO [myActions_User99_Role]
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

62 Posts

Posted - 2012-02-07 : 15:58:23
Brett oh thank you so much.. no wonder the log was disappearing... Also thanks a lot for the logging code.. I will go through it... Thanks alot again.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 16:05:02
Let me know if you need any extra help...some of the local variables are output variables...

I try and make all of the sprocs cookie cutter, so the entire development team does everything the same way, always, for every projetc.

That way, there is no guess workin invold

For Example


CREATE PROC [dbo].[usp_DEL_Category]
@Category_Cd decimal(15,3) -- Category
, @User char(12)
, @rc int OUTPUT, @Error int OUTPUT, @Error_Message varchar(255) OUTPUT, @Rowcount int OUTPUT

AS


The items in Red are in EVERY Sproc.

And there is always ONLY 1 of each..BEGIN TRAN, COMMIT TRAN and ROLLBACK TRAN

We ALWAYS Only use Fall through logic, and ONLY Use GOTOs to direct the flow to an error routine

Good Luck

Here's a sample of an entire sproc


USE [myActions]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_DEL_Category]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_DEL_Category]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[usp_DEL_Category]
@Category_Cd decimal(15,3) -- Category
, @User char(12)
, @rc int OUTPUT, @Error int OUTPUT, @Error_Message varchar(255) OUTPUT, @Rowcount int OUTPUT
AS


--
-- Enterprise Solutions
--
-- File: \\paerscns20\ss\GBTS_Systems_Projects\Projects\myActions\Database\SQLServer\NJROS1BBLD0304\DEV2K08\Sprocs
-- Date: 09/29/2011
-- Author: Sarah Abdallah
-- Server: NJROS1BBLD0304\DEV2K08
-- Database: myActions
-- Login: myActions_User99
-- Procedure: usp_DEL_Category
-- Description: Delete a Row in the Category table
--
-- Return codes: -1 Unsuccessful Execution
-- 0 Successful Execution
-- 1 Zero rows returned or modified
--
-- Tables Used: Category
--
-- Tables Created: None
--
--
-- Row Estimates:
-- name rows reserved data index_size unused
-- -------------------- ----------- ------------------ ------------------ ------------------ ------------------
-- Message_Code 3 16 KB 8 KB 8 KB 0 KB
--
-- sp_spaceused Category
--
--Change Log
--
-- UserId Date Description
-- ----------- -------------- ------------------------------------------------------------------------------------------
-- X163043 09/29/2011 Initial Release
--




/* Sample Execution:

--SELECT * FROM Category

--Insert test row to be deleted first.

DECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int;
EXEC usp_INS_Category
@Category_Cd =6.6
, @Category_Name ='XXX'
, @Category_Desc ='XXX'
, @User ='X002548'
, @rc=@rc OUTPUT, @Error=@Error OUTPUT, @Error_Message=@Error_Message OUTPUT, @Rowcount=@Rowcount OUTPUT;
SELECT @rc AS [RC], @Error AS [Error], @Error_Message AS [Error_Message], @Rowcount AS [RowCount]


-- Test Delete

DECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int;
EXEC usp_DEL_Category
@Category_Cd =6.6
, @User ='X002548'
, @rc=@rc OUTPUT, @Error=@Error OUTPUT, @Error_Message=@Error_Message OUTPUT, @Rowcount=@Rowcount OUTPUT;
SELECT @rc AS [RC], @Error AS [Error], @Error_Message AS [Error_Message], @Rowcount AS [RowCount]


-- Test Delete of a nonexisting row

DECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int;
EXEC usp_DEL_Category
@Category_Cd =5.0
, @User ='X002548'
, @rc=@rc OUTPUT, @Error=@Error OUTPUT, @Error_Message=@Error_Message OUTPUT, @Rowcount=@Rowcount OUTPUT;
SELECT @rc AS [RC], @Error AS [Error], @Error_Message AS [Error_Message], @Rowcount AS [RowCount]

SELECT * FROM Category

SELECT [Host_Name]
, SYS_USER
, SPROCNAME
, Transtart
, DATEDIFF(ms,Transtart,TranEnd) AS TransLength
, ReturnCode, Rows, ErrorCode, ErrorMessage
, *
FROM SprocExecLog
WHERE SprocName = 'usp_DEL_Category'
ORDER BY 4 DESC


SELECT TOP 1 Parameters
FROM SprocExecLog
WHERE SprocName = 'usp_DEL_Category'
ORDER BY TranStart DESC

*/


SET NOCOUNT ON

BEGIN TRAN

DECLARE @s datetime, @e datetime, @Error_Type int, @Error_Loc int, @sprc int, @rowcount2 int, @Parameters varchar(MAX), @Sprocname varchar(255)

SELECT @rc = 0, @s = GetDate(), @Error = 0, @Error_Message='', @Rowcount = 0, @Rowcount2 = 0, @Sprocname = object_name(@@procid)

DELETE FROM Category
WHERE Category_Cd = @Category_Cd

SELECT @Error = @@ERROR, @Rowcount = @@ROWCOUNT

IF @Error <> 0
BEGIN
SELECT @Error_Loc = 1, @Error_Type = 50001, @rc = -1
GOTO usp_DEL_Category_Err
END

IF @Rowcount = 0
BEGIN
SELECT @Error_Loc = 2, @Error_Type = 50002, @rc = 1
, @Error_Message = 'No Rows Deleted for '
+ 'Category_Cd = ' + '''' + CONVERT(varchar(15),@Category_Cd) + ''''
GOTO usp_DEL_Category_Err
END

SELECT @Rowcount2= @Rowcount2 + @Rowcount

SELECT @Rowcount = @Rowcount2

COMMIT TRAN

usp_DEL_Category_Exit:
SET @e = GetDate()
SET @Parameters = 'DECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int; '+CHAR(13)+CHAR(10)
+ 'EXEC ' + @SprocName +CHAR(13)+CHAR(10)
+ ' @Category_Cd=' +COALESCE( RTRIM(@Category_Cd) ,'NULL')+CHAR(13)+CHAR(10)
+ ', @User=' +COALESCE(CHAR(39)+RTRIM(@User) +CHAR(39),'NULL')+CHAR(13)+CHAR(10)
+ ', @rc=@rc OUTPUT, @Error=@Error OUTPUT, @Error_Message=@Error_Message OUTPUT, @Rowcount=@Rowcount OUTPUT; ' +CHAR(13)+CHAR(10)
+ 'SELECT @rc AS [RC], @Error AS [Error], @Error_Message AS [Error_Message], @Rowcount AS [RowCount]'
EXEC [isp_LOG_SprocExecLog] @Sprocname, @s, @e, @rc, @Error, @Error_Message, @Rowcount, @User, @Parameters, @sprc OUTPUT
SET NOCOUNT OFF
RETURN

usp_DEL_Category_Err:
ROLLBACK TRAN
IF @Error_Type < 50000
RAISERROR @Error_Type @Error_Message
GOTO usp_DEL_Category_Exit

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

GRANT VIEW DEFINITION ON [dbo].[usp_DEL_Category] TO [myActions_User99_Role]
GRANT EXECUTE ON [dbo].[usp_DEL_Category] TO [myActions_User99_Role]
GRANT VIEW DEFINITION ON [dbo].[usp_DEL_Category] TO [myActions_Notes_User99_Role]
GRANT EXECUTE ON [dbo].[usp_DEL_Category] TO [myActions_Notes_User99_Role]
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

62 Posts

Posted - 2012-02-07 : 16:19:51
Thanks a lot Brett... This is really good... I am going through it.
Go to Top of Page

sqlnovice1
Starting Member

34 Posts

Posted - 2012-02-07 : 16:24:02
like the name, good to know there are another 998 novices out there :-)

Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

62 Posts

Posted - 2012-02-07 : 16:25:35
Lol!
quote:
Originally posted by sqlnovice1

like the name, good to know there are another 998 novices out there :-)



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 16:45:39
That number is significantly North of that number

Considering how MUCH SQL Server has "Grown"

I'm not sure I would call BI SQL though..not sure...they are all GUI Tools...so what..you learn a GUI Tool and you can say you know SQL now?

MOO

If it's not transferable to other platforms....



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -