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 2008 Forums
 Transact-SQL (2008)
 rollback not happening !!

Author  Topic 

shantanu88d
Starting Member

35 Posts

Posted - 2011-05-08 : 08:12:56
hi i have this stored procedure. I have written begin tran command. after that i have also written rollback tran and commit tran in the end. still when i run it, SS says that begin tran or commit tran wasnt found !! but those stmts are already there. Pls help


--find previously alotted_qty. It will be used later on to find how much qty was updated
declare @prev_qty as int, @prev_brand as varchar(50), @prev_model_no as varchar(50)
set @prev_qty = (select qty from telephone_alottment where sr_id = @sr_id)
set @prev_brand = (select model_brand from telephone_alottment where sr_id = @sr_id)
set @prev_model_no = (select model_number from telephone_alottment where sr_id = @sr_id)
IF (@qty - @prev_qty)<= (select sum(available_qty) from mobile_master where model_number = @model_number and model_brand = @model_brand)
BEGIN
IF datediff(dd,(select min(date_of_purchase)
from mobile_master
where model_brand = @model_brand and model_number = @model_number),@alottment_date) < 0
BEGIN
set @msg = 'Alottment date cannot be earlier than Purchase Date which is ' + convert(varchar,(select min(date_of_purchase)
from mobile_master
where model_brand = @model_brand and model_number = @model_number),103)
select @msg
END
ELSE
BEGIN
--#################### ADJUST QUANTITY IN MOBILE_MASTER ONLY IF @PREV_ALOTTED_QTY IS DIFFERENT THAN @QTY ################################
--here it means that brand or model no was changed, so we have to return quantity to previous brand or model. then we
--call the proc again to set new value
begin tran
update telephone_alottment
set alottment_group = @alottment_group,
alotted_to = @alotted_to,
model_number = @model_number,
model_brand = @model_brand,
modify_date = getdate(),
alottment_date = @alottment_date,
modified_by = @modified_by,
approved_by = @approved_by,
qty = @qty
where sr_id = @sr_id
IF (@prev_brand <> @model_brand) OR (@prev_model_no <> @model_number)
BEGIN
select 'inside this shit'
exec adjustMobileQtyOnUpdate @prev_qty,'Increase',@prev_model_no,@prev_brand,@alottment_date,@alottment_group,@alotted_to,@msg OUTPUT, @status OUTPUT
exec adjustMobileQtyOnUpdate @qty,'Decrease',@model_number,@model_brand,@alottment_date,@alottment_group,@alotted_to,@msg OUTPUT, @status OUTPUT
IF @status = 0
BEGIN
select @msg
rollback tran
END
END
ELSE
BEGIN
IF @qty <> @prev_qty
BEGIN
IF @prev_qty > @qty
BEGIN
set @qty = @prev_qty - @qty
exec adjustMobileQtyOnUpdate @qty,'Increase',@model_number,@model_brand,@alottment_date,@alottment_group,@alotted_to,@msg OUTPUT, @status OUTPUT
END
ELSE
BEGIN
set @qty = @qty - @prev_qty
exec adjustMobileQtyOnUpdate @qty,'Decrease',@model_number,@model_brand,@alottment_date,@alottment_group,@alotted_to,@msg OUTPUT, @status OUTPUT
END
IF @status = 0
BEGIN
select @msg
return
END
END
END
commit tran
END
END

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-08 : 10:28:02
If you go through the logic of If/Else blocks, it looks like you will get to a rollback tran AND a commit tran - which would result in the error you are seeing. So you have to rearrange logic such that that does not happen. If you could do the following, for example, that would fix the problem - BUT that may not be what you want to accomplish.

....
IF @status = 0
BEGIN
select @msg
rollback tran
return
END
....
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-05-08 : 18:46:53
Usually for something like this you would use a try catch block and raise an error.
what happens if you get an error that issn't shown as a value in @status?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -