I found this code while surfing the web today. Concept is very intersting due to couple of resions.
1. Reduce round trips
2. cocurrency handing
3. More control over the logic
4. it is clear.. !!!!
create proc dbo.Item_Add_toOrderList
(
@orderNumber char(20)
@itemNumber char(20)
@processedBy varchar(64) = null
)
as
declare @errorNumber
-- Test processedBy
if (@processedBy is null) set @processedBy = system_user
-- Transaction
begin tran
-- Test orderNumber (makre sure an appropriate order exists)
if not exists(select OrderID from dbo.Order where (OrderNumber =
@orderNumber))
begin
rollback tran
raiserror ('Item Add failed: orderNumber %s unknown.', 16, 1,
@orderNumber)
return -1
/*
This is the first point at which the transaction will be rolled back if one
of the parameters is erroneous.
*/
end
-- Initialize errorNumber
set @errorNumber = 0
insert dbo.Item
(
orderNumber
,itemNumber
,processedBy
,processedOn
)
select @orderNumber as orderNumber
,@itemNumber as itemNumber
,@processedBy as processedBy
,getdate() as processedOn
-- store current error code
set @errorNumber = @@error
if (@errorNumber != 0)
begin
rollback tran
raiserror ('Item Add failed: an error has occured.', 16, 1)
return @errorNumber
/* Here the transaction is rolled-back if an error occured at insert. */
end
else
begin
commit tran
return 0
/* If no error present transaction is committed. */
end
go
Monday, October 09, 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment