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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 @@ERROR Handeling question

Author  Topic 

erikkl2003
Starting Member

14 Posts

Posted - 2005-12-10 : 19:34:42
Hello sqlers!,

i am in need for someone to point out to me how to handel if i receive an error durning this update

==================================================

I had this little segment in the code and this will not produce desired results.. So i took it out... all of the other checks work fine. but when i add this little snipet sql bypased the othe checks..

Any comments that might help me better understand how to implement this would be very nice... i am open to all comments about this pattern that i am working with..

thanks alot
erik


IF EXISTS (SELECT Email FROM Users WHERE Email = @Admin_Email AND UserType = 'Admin')

BEGIN
UPDATE Users
SET [Password] = @NewUserPassword
Where (Email = @UserEmail And UserID = @UserID)

SELECT @err = @@Error --Check for Update Error
SELECT @rc = @@ROWCOUNT

IF ( @err <> 0 )
BEGIN
GOTO PROBLEM
END
---------------------------------------------------------

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE Admin_ReSet_UserPassword

@Admin_Email Varchar (200), /*Admin Check */
@UserID INT,
@UserEmail VARCHAR (200),
@NewUserPassword Binary (24)

AS
DECLARE @err Varchar (200), @rc as Int

IF EXISTS (SELECT Email FROM Users WHERE Email = @Admin_Email AND UserType = 'Admin')

BEGIN
UPDATE Users
SET [Password] = @NewUserPassword
Where (Email = @UserEmail And UserID = @UserID)

IF (@@ROWCOUNT = 1 )
BEGIN
SET @err = 'Password Change is a Success!'
SELECT @err as lblerror
RETURN
END

IF (@@ROWCOUNT = 0 )
BEGIN
SET @err = 'Unable to change password! Make sure that you have Supplied the correct information!'
SELECT @err as lblerror
RETURN
END
END

ELSE
BEGIN
SET @err = 'Admin Credentials do not Match!'
SELECT @err as lblerror
RETURN
END

PROBLEM:
IF (@err <> 0 )
BEGIN
SET @err = 'There was an error during the update! Please try Again, and if the problem persists please contact the SuperCenter.'
SELECT @err as lblerror
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



If you wait untill tomorrow; you will have no future.

Kristen
Test

22859 Posts

Posted - 2005-12-11 : 03:03:10
@@ERROR and @@ROWCOUNT only hold the value for the immediately preceding statement, so immediately after the statement you want to check you need to do:

SELECT @MyErrNo = @@ERROR, @MyRowCount = @@ROWCOUNT --Note: Single SELECT statement!!

and then do:

IF (@MyRowCount = 1 )
BEGIN
SET @err = 'Password Change is a Success!'
SELECT @err as lblerror
RETURN
END

IF (@MyRowCount = 0 )
BEGIN
SET @err = 'Unable to change password! Make sure that you have Supplied the correct information!'
SELECT @err as lblerror
RETURN
END

is there a good reason why you are doing:

SET @err = 'Unable to change password! Make sure that you have Supplied the correct information!'
SELECT @err as lblerror

instead of:

SELECT 'Unable to change password! Make sure that you have Supplied the correct information!' AS lblerror

Kristen
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-12-11 : 03:07:59
quote:
Originally posted by erikkl2003

Hello sqlers!,

i am in need for someone to point out to me how to handel if i receive an error durning this update

==================================================

I had this little segment in the code and this will not produce desired results.. So i took it out... all of the other checks work fine. but when i add this little snipet sql bypased the othe checks..

Any comments that might help me better understand how to implement this would be very nice... i am open to all comments about this pattern that i am working with..

thanks alot
erik


IF EXISTS (SELECT Email FROM Users WHERE Email = @Admin_Email AND UserType = 'Admin')

BEGIN
UPDATE Users
SET [Password] = @NewUserPassword
Where (Email = @UserEmail And UserID = @UserID)

SELECT @err = @@Error --Check for Update Error
SELECT @rc = @@ROWCOUNT

IF ( @err <> 0 )
BEGIN
GOTO PROBLEM
END
---------------------------------------------------------

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE Admin_ReSet_UserPassword

@Admin_Email Varchar (200), /*Admin Check */
@UserID INT,
@UserEmail VARCHAR (200),
@NewUserPassword Binary (24)

AS
DECLARE @err Varchar (200), @rc as Int

IF EXISTS (SELECT Email FROM Users WHERE Email = @Admin_Email AND UserType = 'Admin')

BEGIN
UPDATE Users
SET [Password] = @NewUserPassword
Where (Email = @UserEmail And UserID = @UserID)

IF (@@ROWCOUNT = 1 )
BEGIN
SET @err = 'Password Change is a Success!'
SELECT @err as lblerror
RETURN
END

IF (@@ROWCOUNT = 0 )
BEGIN
SET @err = 'Unable to change password! Make sure that you have Supplied the correct information!'
SELECT @err as lblerror
RETURN
END
END

ELSE
quote:
I dont think this else is required if no rows are updated then it will be return 0

BEGIN
SET @err = 'Admin Credentials do not Match!'
SELECT @err as lblerror
RETURN
END

PROBLEM:
IF (@err <> 0 )
quote:
It should be like this If (@@Error <> 0)

BEGIN
SET @err = 'There was an error during the update! Please try Again, and if the problem persists please contact the SuperCenter.'
SELECT @err as lblerror
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



If you wait untill tomorrow; you will have no future.



the above are just the suggestions.. if you can post what are you trying to do .. then you can expect the better answers..

Hope its helps..

Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-12 : 01:20:17
More on Error Handlings
http://www.sommarskog.se/error-handling-I.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -