OK, i'm trying to do some error checking on stored procedures and am
following the advise in Erland Sommarskog's 'Implementing Error
Handling with Stored Procedures' document.
Can anybody help with my stored procedures and why it keeps erroring at
the '-- Create new Address Detail stage'? The errorCode value that is
being return in my web app is 0, so i'm not even sure why it's even
raising the error!!
Rather than executing the INSERT INTO AddressDetail in my
CreateSupplier procedure and checking for errors, i'd like to be able
execute a CreateAddressDetail SP, so that i can reuse it throughout my
web app.
New suppliers must have a contact address associated with it, so if
there's an error creating the suppliers address, i need my
CreateSupplier stored procedure to ROLLBACK and not create the new
supplier. That's why i'm not doing two separate calls to the procedures
from my app code.
Any suggestions are most appreciated.
Many thanks
Dan Williams.
CREATE PROCEDURE CreateSupplier
@supplierName varchar(50),
@userId bigint,
@address varchar(50),
@town varchar(50),
@county varchar(50),
@postCode varchar(15),
@contactName varchar(50)
AS
BEGIN
DECLARE @newSupplierId as bigint
DECLARE @newAddressDetailId as bigint
DECLARE @errorCode as bigint
SET NOCOUNT ON
BEGIN TRAN
INSERT INTO Supplier
(supplierName, accOpenedBy, accOpenedDate)
VALUES (@supplierName, @userId, getDate())
SET @newSupplierId = SCOPE_IDENTITY()
-- Check for an error creating new supplier
SELECT @errorCode = @@ERROR
IF (@errorCode <> 0) BEGIN ROLLBACK TRAN RAISERROR ('Error creating
supplier',16,1) RETURN @errorCode END
-- Create new Address Detail
EXEC @errorCode = CreateAddressDetail @address, @town, @county,
@postCode, @contactName, @newAddressDetailId OUTPUT
SELECT @errorCode = coalesce(nullif(@errorCode, 0), @@error)
if @errorCode <> 0 BEGIN ROLLBACK TRAN RAISERROR ('Error creating
address. ErrorCode = %d',16, @errorCode) RETURN @errorCode END
COMMIT TRAN
SET NOCOUNT OFF
RETURN @newSupplierId
END
GO
CREATE PROCEDURE CreateAddressDetail
@address varchar(50),
@town varchar(50),
@county varchar(50),
@postCode varchar(15),
@contactName varchar(50),
@newAddressDetailId bigint OUTPUT
AS
BEGIN
-- Create new AddressDetail
DECLARE @errorCode as bigint
SET NOCOUNT ON
BEGIN TRAN
INSERT INTO AddressDetail
(address, town, county, postCode, contactName)
VALUES (@address, @town, @county, @postCode, @contactName)
SET @newAddressDetailId = SCOPE_IDENTITY()
-- Check for an error creating new address
SELECT @errorCode = @@ERROR
IF (@errorCode <> 0)
BEGIN
RAISERROR ('Error creating new address detail',16,1)
ROLLBACK TRAN
END
ELSE
COMMIT TRAN
SET NOCOUNT OFF
RETURN @newAddressDetailId
END
GO 9 10276
Hi
Look at http://www.sommarskog.se/error-handling-II.html
Regards
--------------------------------
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/
"dt********@hotmail.com" <da**********@newcross-nursing.com> wrote in
message news:11**********************@g14g2000cwa.googlegr oups.com... OK, i'm trying to do some error checking on stored procedures and am following the advise in Erland Sommarskog's 'Implementing Error Handling with Stored Procedures' document.
Can anybody help with my stored procedures and why it keeps erroring at the '-- Create new Address Detail stage'? The errorCode value that is being return in my web app is 0, so i'm not even sure why it's even raising the error!!
Rather than executing the INSERT INTO AddressDetail in my CreateSupplier procedure and checking for errors, i'd like to be able execute a CreateAddressDetail SP, so that i can reuse it throughout my web app.
New suppliers must have a contact address associated with it, so if there's an error creating the suppliers address, i need my CreateSupplier stored procedure to ROLLBACK and not create the new supplier. That's why i'm not doing two separate calls to the procedures from my app code.
Any suggestions are most appreciated.
Many thanks
Dan Williams. CREATE PROCEDURE CreateSupplier @supplierName varchar(50), @userId bigint, @address varchar(50), @town varchar(50), @county varchar(50), @postCode varchar(15), @contactName varchar(50) AS BEGIN
DECLARE @newSupplierId as bigint DECLARE @newAddressDetailId as bigint DECLARE @errorCode as bigint
SET NOCOUNT ON
BEGIN TRAN
INSERT INTO Supplier (supplierName, accOpenedBy, accOpenedDate) VALUES (@supplierName, @userId, getDate())
SET @newSupplierId = SCOPE_IDENTITY()
-- Check for an error creating new supplier SELECT @errorCode = @@ERROR IF (@errorCode <> 0) BEGIN ROLLBACK TRAN RAISERROR ('Error creating supplier',16,1) RETURN @errorCode END
-- Create new Address Detail EXEC @errorCode = CreateAddressDetail @address, @town, @county, @postCode, @contactName, @newAddressDetailId OUTPUT
SELECT @errorCode = coalesce(nullif(@errorCode, 0), @@error)
if @errorCode <> 0 BEGIN ROLLBACK TRAN RAISERROR ('Error creating address. ErrorCode = %d',16, @errorCode) RETURN @errorCode END
COMMIT TRAN SET NOCOUNT OFF RETURN @newSupplierId
END GO CREATE PROCEDURE CreateAddressDetail @address varchar(50), @town varchar(50), @county varchar(50), @postCode varchar(15), @contactName varchar(50), @newAddressDetailId bigint OUTPUT
AS BEGIN
-- Create new AddressDetail
DECLARE @errorCode as bigint
SET NOCOUNT ON
BEGIN TRAN
INSERT INTO AddressDetail (address, town, county, postCode, contactName) VALUES (@address, @town, @county, @postCode, @contactName)
SET @newAddressDetailId = SCOPE_IDENTITY()
-- Check for an error creating new address SELECT @errorCode = @@ERROR IF (@errorCode <> 0) BEGIN RAISERROR ('Error creating new address detail',16,1) ROLLBACK TRAN END ELSE COMMIT TRAN SET NOCOUNT OFF RETURN @newAddressDetailId END GO
Er.... I already have. That's the article i referenced in my original
post.
I am not a transaction pro or anything but perhaps I can point you in
the right direction, more experienced developers may eventually be more
helpful.
I could be wrong but I think in your CreateAddressDetail proc you
should not have the commit inside the else and since you are using
CreateAddressDetail inside another transaction you may want to label
your transaction and monitor transcount.
I may be wrong on both points but it may pay to check either way.
I am not a transaction pro or anything but perhaps I can point you in
the right direction, more experienced developers may eventually be more
helpful.
I could be wrong but I think in your CreateAddressDetail proc you
should not have the commit inside the else and since you are using
CreateAddressDetail inside another transaction you may want to label
your transaction and monitor transcount.
I may be wrong on both points but it may pay to check either way. dt********@hotmail.com (da**********@newcross-nursing.com) writes: OK, i'm trying to do some error checking on stored procedures and am
INSERT INTO Supplier (supplierName, accOpenedBy, accOpenedDate) VALUES (@supplierName, @userId, getDate())
SET @newSupplierId = SCOPE_IDENTITY()
-- Check for an error creating new supplier SELECT @errorCode = @@ERROR
No, you are checking for error an error when retrieving the value from
SCOPE_IDENTITY(). Which never fails, so you will always get 0.
@@error is set after *every* statement.
This is why I always write my code as:
INSERT INTO Supplier (supplierName, accOpenedBy, accOpenedDate)
VALUES (@supplierName, @userId, getDate())
SELECT @err = @@error IF @err <> 0 RETURN @err
And then there is a space to the next statement. That is, conceptually
I view the error-checking bit as part of the statment it belongs to.
Oh! So much easier this will be in SQL 2005!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Erland Sommarskog wrote: Oh! So much easier this will be in SQL 2005!
Why do you say that?
It is horrible now and I don't see anything new coming to T-SQL
that will make it any less so.
--
Daniel A. Morgan http://www.psoug.org da******@x.washington.edu
(replace x with u to respond)
OK, thanks for all the replies.
In the end, it wasn't just the fact that i needed to set by @errorCode
immediately after.
The problem was my CreateAddressDetail stored procedure i execute from
my CreateSupplier procedure was returning a new @newAddressDetailId
scope identity, hence causing my @errorCode to be greater than zero and
raising an error. I now just rely on using an OUTPUT variable.
Cheers
Dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bill S. |
last post by:
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...
|
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...
|
by: Rhino |
last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to
write down a few "best practices" for reference so that I will have them
handy for future development.
Would the...
|
by: Rhino |
last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a
Throwable back to the calling program as an OUT parameter?
If yes, what datatype should I use when registering the...
|
by: Jeff |
last post by:
I have question about differences in fenced sql procedures and fenced
stored procedures. Do fenced sql procedures take up an extra memory
segment when executed? Reason I ask is we have several...
|
by: Raquel |
last post by:
This is a very simple DB2 SQLJ stored procedure. The problem is that it
seems to run fine but returns NOTHING. I mean..as if nothing has
happened..not resultset is returned. I am passing value...
|
by: Kent Lewandowski |
last post by:
hi all,
Recently I wrote some stored procedures using java jdbc code
(admittedly my first stab) and then tried to implement the same within
java packages (for code reuse). I encountered...
|
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...
|
by: barmatt80 |
last post by:
I am stumped on the error reporting with sql server. I was told i
need to return @SQLCode(code showing if successful or not) and
@ErrMsg(and the message returned). I am clueless on this.
I...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |