Friday, March 9, 2012

Please tell me why @@Error is not picking up the error

in this procedure the Gender column is a bit datatype. as you can see i'am adding a string.

when i exec proc my error message "print 'error'" is not showing. please help.

you can email at cbmorton!at!gmail.com

CREATE PROCEDURE [dbo].[sp_newuserregistration] AS
BEGIN TRAN
INSERT INTO UserLoginInfo (UserName,[Password]) values ('?','?')
SELECT @.@.IDENTITY AS 'Identity'
DECLARE @.UserID AS INT
SET @.UserID = @.@.Identity
INSERT INTO UserAccountInfo (UserID, Email) VALUES (@.UserID, '?')
INSERT INTO UserProfileInfo (UserID, FirstName, LastName, Gender, DateOfBirth) VALUES (@.UserID, '?', '?', 'x', null)
IF @.@.ERROR <> 0
BEGIN
PRINT 'error'

ROLLBACK TRAN
END
ELSE
BEGIN
COMMIT TRAN
END


GO

--EXEC sp_newuserregistration

The batch stops executing on the error and you can not catch it. You will need to use try/catch but it is only available in SQL Server 2005.|||? As Andreas mentioned, this is a batch-aborting exception -- for this reason, I usually like to use SET XACT_ABORT in my stored procedures that use transactions. Setting that option to ON will cause the transaction to automatically roll back in case of an exception. You might want to read the following articles for more background on all of this: http://www.sommarskog.se/error-handling-I.html http://www.sommarskog.se/error-handling-II.html -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <codrakon@.discussions.microsoft.com> wrote in message news:2db52d22-6890-435c-92c1-e794a3077677@.discussions.microsoft.com... in this procedure the Gender column is a bit datatype. as you can see i'am adding a string. when i exec proc my error message "print 'error'" is not showing. please help. you can email at cbmorton!at!gmail.com CREATE PROCEDURE [dbo].[sp_newuserregistration] ASBEGIN TRANINSERT INTO UserLoginInfo (UserName,[Password]) values ('?','?')SELECT @.@.IDENTITY AS 'Identity'DECLARE @.UserID AS INTSET @.UserID = @.@.Identity INSERT INTO UserAccountInfo (UserID, Email) VALUES (@.UserID, '?')INSERT INTO UserProfileInfo (UserID, FirstName, LastName, Gender, DateOfBirth) VALUES (@.UserID, '?', '?', 'x', null)IF @.@.ERROR <> 0BEGINPRINT 'error' ROLLBACK TRANENDELSEBEGINCOMMIT TRANEND GO --EXEC sp_newuserregistration

No comments:

Post a Comment