473,379 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Sql Server incorrectly functioning

I have a .NET application with a sql server database backend. Our
client has been having problems lately with data being returned from
the database with wrong data. We have error logging in our stored
procedures and even this is reporting wrong. In every stored procedure
we have the following:

IF @@Error <> 0
BEGIN
EXEC dbo.HandleError
@ErrorNum = @@Error,
@ProcID = @@PROCID
END

Then, HandleError looks like:

CREATE PROCEDURE dbo.HandleError
@ErrorNum int,
@ProcID int
AS
/************************************************** *****************
* PROCEDURE: HandleError
* PURPOSE: Handles logging an error
* CALLED FM: Other stored procedures
************************************************** ******************/

SET NOCOUNT ON

DECLARE @UserID int
DECLARE @Host varchar(50)
DECLARE @Len int
DECLARE @SPName VARCHAR(100)
DECLARE @ErrorMsg varchar(500)
DECLARE @ErrorTrace varchar(4000)

-- Get our host name
SET @Host = HOST_NAME()
SET @Len = LEN(@Host) - CHARINDEX(':', @Host)

IF @Len > 0 AND CHARINDEX(':', @Host) > 0
SET @UserID = RIGHT(@Host, LEN(@Host) - CHARINDEX(':', @Host))
ELSE
SET @UserID = NULL

SET @SPName = OBJECT_NAME(@ProcID)

SET @ErrorMsg = 'SQL Error: ' + @SPName

SET @ErrorTrace = 'Error: '
+ CAST(@ErrorNum AS VARCHAR(50))

EXEC dbo.InsertApplicationErrors
@ErrorMessage = @ErrorMsg,
@ExceptionMessage = '',
@ErrorStackTrace = @ErrorTrace,
@UserID = @UserID,
@HostID = @Host,
@Logfile = '';

SET NOCOUNT OFF

GO

InsertApplicationErrors inserts a log into a table we have that we
monitor. We got an error back in the ErrorStackTrace column with
'Error: 0'. That shouldn't happen, as the only time we log error is if
it is not 0.

Does anyone have any ideas on what might be wrong?

Steve

Jul 23 '05 #1
7 1251
Hi

You need to check for the value of @@error after every statement in a batch
or SP, not just once at the end.

There is a problem with your error handler. @@error gets reset after every
statement, so your IF @@Error <> 0 is in effect setting it back to 0.

You need to change it to the following:

SET @LocalErrorVar = @@Error
IF @LocalErrorVar <> 0
BEGIN
EXEC dbo.HandleError
@ErrorNum = @LocalErrorVar,
@ProcID = @@PROCID
END
--
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mi**@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"Steve" <fi*******@yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I have a .NET application with a sql server database backend. Our
client has been having problems lately with data being returned from
the database with wrong data. We have error logging in our stored
procedures and even this is reporting wrong. In every stored procedure
we have the following:

IF @@Error <> 0
BEGIN
EXEC dbo.HandleError
@ErrorNum = @@Error,
@ProcID = @@PROCID
END

Then, HandleError looks like:

CREATE PROCEDURE dbo.HandleError
@ErrorNum int,
@ProcID int
AS
/************************************************** *****************
* PROCEDURE: HandleError
* PURPOSE: Handles logging an error
* CALLED FM: Other stored procedures
************************************************** ******************/

SET NOCOUNT ON

DECLARE @UserID int
DECLARE @Host varchar(50)
DECLARE @Len int
DECLARE @SPName VARCHAR(100)
DECLARE @ErrorMsg varchar(500)
DECLARE @ErrorTrace varchar(4000)

-- Get our host name
SET @Host = HOST_NAME()
SET @Len = LEN(@Host) - CHARINDEX(':', @Host)

IF @Len > 0 AND CHARINDEX(':', @Host) > 0
SET @UserID = RIGHT(@Host, LEN(@Host) - CHARINDEX(':', @Host))
ELSE
SET @UserID = NULL

SET @SPName = OBJECT_NAME(@ProcID)

SET @ErrorMsg = 'SQL Error: ' + @SPName

SET @ErrorTrace = 'Error: '
+ CAST(@ErrorNum AS VARCHAR(50))

EXEC dbo.InsertApplicationErrors
@ErrorMessage = @ErrorMsg,
@ExceptionMessage = '',
@ErrorStackTrace = @ErrorTrace,
@UserID = @UserID,
@HostID = @Host,
@Logfile = '';

SET NOCOUNT OFF

GO

InsertApplicationErrors inserts a log into a table we have that we
monitor. We got an error back in the ErrorStackTrace column with
'Error: 0'. That shouldn't happen, as the only time we log error is if
it is not 0.

Does anyone have any ideas on what might be wrong?

Steve

Jul 23 '05 #2
Steve (fi*******@yahoo.com) writes:
I have a .NET application with a sql server database backend. Our
client has been having problems lately with data being returned from
the database with wrong data. We have error logging in our stored
procedures and even this is reporting wrong. In every stored procedure
we have the following:

IF @@Error <> 0
BEGIN
EXEC dbo.HandleError
@ErrorNum = @@Error,
@ProcID = @@PROCID
END


I've bad news for you: you have lots of stored procedures to edit.
@@error is set after each statement, and that includes IF. So by the
time execution reaches the EXEC statement @@error is 0.

I have an article on my web site about error handling in stored procedures
that you may find useful, http://www.sommarskog.se/error-handling-II.html.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
Steve wrote:
I have a .NET application with a sql server database backend. Our
client has been having problems lately with data being returned from
the database with wrong data. We have error logging in our stored
procedures and even this is reporting wrong. In every stored procedure
we have the following:

IF @@Error <> 0
BEGIN
EXEC dbo.HandleError
@ErrorNum = @@Error,
@ProcID = @@PROCID
END

Then, HandleError looks like:

CREATE PROCEDURE dbo.HandleError
@ErrorNum int,
@ProcID int
AS
/************************************************** *****************
* PROCEDURE: HandleError
* PURPOSE: Handles logging an error
* CALLED FM: Other stored procedures
************************************************** ******************/

SET NOCOUNT ON

DECLARE @UserID int
DECLARE @Host varchar(50)
DECLARE @Len int
DECLARE @SPName VARCHAR(100)
DECLARE @ErrorMsg varchar(500)
DECLARE @ErrorTrace varchar(4000)

-- Get our host name
SET @Host = HOST_NAME()
SET @Len = LEN(@Host) - CHARINDEX(':', @Host)

IF @Len > 0 AND CHARINDEX(':', @Host) > 0
SET @UserID = RIGHT(@Host, LEN(@Host) - CHARINDEX(':', @Host))
ELSE
SET @UserID = NULL

SET @SPName = OBJECT_NAME(@ProcID)

SET @ErrorMsg = 'SQL Error: ' + @SPName

SET @ErrorTrace = 'Error: '
+ CAST(@ErrorNum AS VARCHAR(50))

EXEC dbo.InsertApplicationErrors
@ErrorMessage = @ErrorMsg,
@ExceptionMessage = '',
@ErrorStackTrace = @ErrorTrace,
@UserID = @UserID,
@HostID = @Host,
@Logfile = '';

SET NOCOUNT OFF

GO

InsertApplicationErrors inserts a log into a table we have that we
monitor. We got an error back in the ErrorStackTrace column with
'Error: 0'. That shouldn't happen, as the only time we log error is if
it is not 0.

Does anyone have any ideas on what might be wrong?

Steve


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You have to set a variable to hold the @@error value, 'cuz doing this:

IF @@error <> 0

causes the @@error value to reset to zero (0). E.g.:

If @@error <> 0
print @@error

will return 0 for the "print @@error" statement.

So, do it this way:

DECLARE @err INT
SET @err = @@error
IF @err <> 0
print @err

will return the error number for the "print @err" statement.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQpTckYechKqOuFEgEQJWHwCfX7UXm/Z+2G45a29oZHy9fA1Z1i4AnAiB
G+K2wPgJC4EKqYIZsgI4x5rQ
=G9He
-----END PGP SIGNATURE-----
Jul 23 '05 #4
Thanks, I will give a good read through and rework this. Not too happy
with Microsoft's documentation, as they state I should be able to do
this. From Sql Server Books Online...

-- Save any non-zero @@ERROR value.
IF (@@ERROR <> 0)
SET @ErrorSave = @@ERROR

Found in the using @@ERROR section.

UGH! You aren't kidding, we have over 1000 SPs I get to re-edit and
change where we are check for the errors.

Jul 23 '05 #5
Steve (fi*******@yahoo.com) writes:
Thanks, I will give a good read through and rework this. Not too happy
with Microsoft's documentation, as they state I should be able to do
this. From Sql Server Books Online...

-- Save any non-zero @@ERROR value.
IF (@@ERROR <> 0)
SET @ErrorSave = @@ERROR

Found in the using @@ERROR section.


See below for a link to an updated version of Books Online. It is indeed
an embarrssing error. But the same section also says:

Because @@ERROR is cleared and reset on each statement executed, check
it immediately following the statement validated, or save it to a local
variable that can be checked later.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
Erland Sommarskog (es****@sommarskog.se) writes:
Steve (fi*******@yahoo.com) writes:
Thanks, I will give a good read through and rework this. Not too happy
with Microsoft's documentation, as they state I should be able to do
this. From Sql Server Books Online...

-- Save any non-zero @@ERROR value.
IF (@@ERROR <> 0)
SET @ErrorSave = @@ERROR

Found in the using @@ERROR section.


See below for a link to an updated version of Books Online. It is indeed
an embarrssing error. But the same section also says:

Because @@ERROR is cleared and reset on each statement executed, check
it immediately following the statement validated, or save it to a local
variable that can be checked later.


By the way, while I don't want to embarrass you, I would like to point
out that this mishap could have been avoided if you had tested the
error handling, before you spread it all over town. Error-handling code
is one that is often over-looked when testing, but good testing should
also cover some error cases to see that they are handled well.

On a completely other note: SQL 2005 gives you exception handling with
TRY-CATCH syntax, which makes error handling in SQL 2005 a lot more
pleasant.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Well, in theory I did test the error handling. I suppose I didn't go
an execute code that would cause an error, I simply set our error we
wanted logged. I suppose in hind sight, testing with causing an error
would have been better than just saying our error is 1. But, then
again, who expects the documentation to be wrong?

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jim | last post by:
I've just bought a new Windows Server 2003 and I am trying to move an exiting FrontPage website to this new machine. I can't get ASP pages to run. IIS 6.0 has been installed. From the snap-in,...
2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
10
by: B Moor | last post by:
Host PC: Win SBS 2003 with 2.6 GHz processor and 1GB RAM SQL Server 2000 sp3 (or latest).MS Office 2k3 I have the access 2k3 front end running on server (this may get changed) and all was well...
1
by: B Moor | last post by:
Hello, I am quite bogged down with this problem and would like some tips/help if any one has any. Thanks in advance. The Problem ----------- This system initially seemed quite stable for...
5
by: moondaddy | last post by:
I'm setup with a new ISP where we were given an account which we could setup multiple domains. To do this we were given a root directory where we setup a folder for each domain. then we mapped...
9
by: RvGrah | last post by:
After much hair-pulling, I've finally found the answer to a problem that many are fighting with, difficulty connecting from Sql 2005 Server Management or VS2005 to a remote Sql Server running Sql...
1
by: georgewbaba | last post by:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <script type="text/javascript"> function addpath(obj ,vari){ ...
4
by: apragent | last post by:
Our web server crashed and I had to move all web sites to backup server. Installed Internet service and it appears to work OK on html files, but ASP files are not functioning - what is wrong? I...
2
by: aaron | last post by:
Hi All, I have a custom web control which works perfectly. Its just a tabcontrol to spit out navigation tabs, it inherits from System.Web.UI.WebControls.WebControl In web page design view,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.