-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create one StoredProcedure for SQL Try-Catchs #54
Comments
یاشا کیشی -----Original Message----- When us want to run a SP, maybe that codes have some problem and cause to throw an errors in the SQL. So this is better to have one Stored Procedures to log the exception informations on the ErrorLog table, without to know what is codes exist in the SP bodies. |
One SP by name 'sp_CatchError' created to catch any SQL Error without passing parameters. sp_CatchError Body is: CREATE PROCEDURE [dbo].[sp_CatchError]
@RaisError bit
AS
BEGIN
DECLARE
@DatabaseName NVARCHAR(max) = IsNull(Original_DB_NAME(), DB_NAME()),
@ERROR_NUMBER INT = ERROR_NUMBER() , -- @@ERROR
@ERROR_STATE INT = ERROR_STATE() ,
@ERROR_SEVERITY INT = ERROR_SEVERITY(),
@ERROR_LINE INT = ERROR_LINE() ,
@ERROR_Column INT = 0,
@ERROR_PROCEDURE SysName = ERROR_PROCEDURE() ,
@ERROR_MESSAGE NVARCHAR(max) = ERROR_MESSAGE(),
@Server_Instance NVARCHAR(1024) = @@SERVERNAME + '' \ '' + @@ServiceName,
@IP_Address SysName = (SELECT client_net_address FROM SYS.DM_EXEC_CONNECTIONS WHERE SESSION_ID = @@SPID),
@MAC_Address SysName = (SELECT net_address from sysprocesses where spid = @@SPID),
@Culture SysName = @@LANGUAGE,
@OS NVARCHAR(max) = @@Version,
@ClrVersion SysName = (SELECT CONVERT(sysname, SERVERPROPERTY(''BuildClrVersion''))),
@ErrorDate DateTime = GetDate(),
@IsHandled bit = 1,
@ErrorType SysName = ''SqlException'',
@UserName SysName = suser_sname(),
@MemberType SysName = ''Stored Procedure'';
IF @ERROR_NUMBER <> 50000
-- Check the error exist or not? if exist then only update that
IF ( Select COUNT(ErrorId) FROM [ErrorLog]
WHERE HResult = @ERROR_NUMBER AND
Line = @ERROR_LINE AND
Method = @ERROR_PROCEDURE AND
[User] = @UserName) > 0
-- Update error object from ErrorLog table
UPDATE dbo.ErrorLog SET DuplicateNo += 1
WHERE
HResult = @ERROR_NUMBER AND
Line = @ERROR_LINE AND
Method = @ERROR_PROCEDURE AND
[User] = @UserName;
ELSE
BEGIN
INSERT INTO UsersManagements.dbo.ErrorLog
(
[OS],
[User],
[CLRVersion],
[ErrorDateTime],
[IsHandled],
[Type],
[Line],
[Column],
[Message],
[HResult],
[Source],
[Method],
[ModuleName],
[IPv4Address],
[MACAddress],
[MemberType],
[CurrentCulture],
[DuplicateNo],
[Data]
)
VALUES (
@OS,
@UserName,
@ClrVersion,
@ErrorDate,
@IsHandled,
@ErrorType,
@ERROR_LINE,
@ERROR_Column,
@ERROR_MESSAGE,
@ERROR_NUMBER,
@DatabaseName,
@ERROR_PROCEDURE,
@Server_Instance,
@IP_Address,
@MAC_Address,
@MemberType,
@Culture,
0,
(
SELECT
@ERROR_SEVERITY [SEVERITY],
@ERROR_STATE [STATE]
FOR
XML PATH('''') ,
ROOT(''Error'')
)
)
END
If @RaisError = 1
RAISERROR(@ERROR_MESSAGE, 18, 255)
END |
How to use this SPOne Stored Procedure example with sp_CatchError usage: USE [TestDB]
GO
/****** Object: StoredProcedure [dbo].[sp_TestThrowError] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_TestThrowError]
As
BEGIN
BEGIN TRY
BEGIN TRANSACTION
DECLARE @None INT;
SET @None = 1 / 0; -- THROW 40000, 'Divide by zero exception' , 1
RETURN @None
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
Exec UsersManagements.dbo.sp_CatchError @RaisError = 0 -- RaisError or Not
END CATCH
END |
as an aside, How can I send my custom UPDATE:
|
Yes, is good idea to add custom data into error log table by this SP. i try it again... |
One Parameters by name For example to use that: Begin TRY
-- Throw one exception to test
SELECT 1 /0
END TRY
Begin CATCH
EXEC UsersManagements.dbo.sp_CatchError @RaisError = 0, -- bit
@ExtraData = N'test d' -- nvarchar(max)
END Catch |
Thanks a lot 👍 |
Yes, SP must be return inserted record ID to the user procedures. |
Thank you :)
and the consider to doing something like this:
|
Thank you too. |
Do you think is better than it was a scalar function? |
If you mean using |
Actually there are some limitations on scalar function, like Its IMPOSSIBLE to Insert into table in a scalar function. |
OK, so i used OUTPUT arguments to return ID. |
Now you can use this model: BEGIN TRY
-- Throw Divid by zero exception for test
SELECT 1 / 0
END TRY
Begin CATCH
DECLARE @id BIGINT
EXECUTE UsersManagements.dbo.sp_CatchError @RaisError = NULL, -- bit
@ExtraData = N'test', -- nvarchar(max)
@ErrorId = @id OUTPUT -- bigint
SELECT * FROM UsersManagements.dbo.ErrorLog WHERE ErrorId = @id
END CATCH |
thank you Behzad, as we talked before, it's impossible to access local functions like |
I try to add this feature as soon. thanks a lot for your attentions |
When us want to run a SP, maybe that codes have some problem and cause to throw an errors in the SQL. So this is better to have one Stored Procedures to log the exception informations on the ErrorLog table, without to know what is codes exist in the SP bodies.
The text was updated successfully, but these errors were encountered: