473,405 Members | 2,404 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,405 software developers and data experts.

Help: Prob handling SQL error in stored proc

Hi,

I a stored procedure that inserts a record into a table as
below.

The insert works OK, but if the insert violates a unique
indewx constraint on one of the columns, the proc terminates
immediately, and does NOT execute the 'if @@ERROR <> 0'
statement.

Am I doing something wrong, or do I need to set an attribute
somewhere?

tia,
Bill

begin tran

insert into Users
(UserName, UserPWD, Lname, Fname, UserDesc)
values (@userName, @userPWD, @lname, @fname, @userDesc)

if @@ERROR <> 0
begin
rollback tran
set @returnCode = -2
set @errMsg = 'SQL error '
+ convert(varchar(6), @@ERROR)
+ ' occurred adding user '
+ @userName
end
Jul 20 '05 #1
1 4759
[posted and mailed, please reply in news]

Bill S. (bi*********@hotmail.com) writes:
I a stored procedure that inserts a record into a table as
below.

The insert works OK, but if the insert violates a unique
indewx constraint on one of the columns, the proc terminates
immediately, and does NOT execute the 'if @@ERROR <> 0'
statement.

Am I doing something wrong, or do I need to set an attribute
somewhere?
begin tran

insert into Users
(UserName, UserPWD, Lname, Fname, UserDesc)
values (@userName, @userPWD, @lname, @fname, @userDesc)

if @@ERROR <> 0
begin
rollback tran
set @returnCode = -2
set @errMsg = 'SQL error '
+ convert(varchar(6), @@ERROR)
+ ' occurred adding user '
+ @userName
end


First, @@error is set after each statement, so @errMsg will never read
anything but "SQL Error 0 ...". Always save @@error in a local variable
before you do anything else with it.

So over to your question. Error handling in SQL Server is a messy topic,
and there are errors you cannot trap like this, because SQL Server
aborts the batch immediately. However, constraint violation as you
mention is not among those - unless the setting SET XACT_ABORT is ON.

So, assuming you are not using XACT_ABORT ON, the error handler should
be executed. But how do you know that it is not? What are @returnCode
and @errMsg? Local variables? Output parameters? If they are output
parameters, and run the procedure from Query Analyzer:

DECLARE @ret int, @errMsg varchar(200)
EXEC your_sp @userName, ..., @ret OUTPUT, @errMsg OUTPUT
SELECT @ret, @errMsg

My guess is that you are running the procedure from some client library
which traps the error, before you get to read the output parameters.

As I mentioned, error handling is really a messy topic, but I have
an article on error handling of my web site that may be of interest,
http://www.sommarskog.se/error-handling-II.html. I don't know exactly
on what level you are on; if you are fairly unexperienced with SQL
Server, you may feel overwhelmed, but you could browse it now, and
save it for later reading.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

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

Similar topics

4
by: vani | last post by:
HI All, We are working on a search application, where the number of records in the database are in millions. For certain search conditions, the search result results in more than million...
0
by: Jon LaRosa | last post by:
Hi all - I have a web application and I want to be able to do some basic error handling. For example, here is one error I would like to catch and display in a useful way for the user:...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
2
by: Mike | last post by:
Greetings, Having a major problem here. running version 8.2 on win2003 server. The problem I am having is backing up a database seems to get to the last part of the backup and then fails. This...
4
by: William F. Robertson, Jr. | last post by:
One of my developers came to me with this question and I don't have an answer for them. The only suggestion I had for them was to change the return to a output parameter and put a try catch around...
8
by: CarpetMnuncher! | last post by:
================================================================= How do I use Try Catch error handling when a timer is involved? If I preform the preciduer below and I get an error I revive...
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
0
by: db2user24 | last post by:
I'm trying to invoke a DB2 stored procedure. The stored proc is coded in C and compiled to a shared library, which has been placed in the <DB2 dir>/functions directory. The platform is Linux (using...
0
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.