【发布时间】:2016-03-31 15:18:26
【问题描述】:
我正在学习 TSQL,但在尝试更新产品表中的列时出现错误。它告诉我 COMMIT TRANSACTION 请求没有相应的 BEGIN TRANSACTION,但我的存储过程顶部有一个开始事务。任何帮助表示赞赏。
alter proc dbo.ProductOrders
@ProdId int, @ProductPrice smallmoney
as
Declare @timesOrdered int, @ProductId int,
Declare @counter int, @return_value int
Declare @time timestamp, @irowCount int
set transaction isolation level read uncommitted
set nocount on
set @counter = 0
while(@counter < 3)
begin
begin transaction
select @ProductId = ProductId, @ProductPrice = UnitPrice, @time = ProductStamp
from dbo.Products
where ProductId = @ProdId
select @timesOrdered = COUNT(ProductId)
from dbo.Products
where ProductId = @ProdId
if(@timesOrdered < 2)
begin
raiserror('Product hasnt been ordered enough to raise price',16,1)
rollback transaction
break
end
EXEC @return_value = [dbo].[UpdateProduct]
@ProductPrice = @ProductPrice,
@ProdId = @ProdId,
@time = @time,
@eRowCount = @irowCount OUTPUT
SELECT 'Return Value' = @return_value
if @return_value <> 0
begin
raiserror ('Product not updated, error occured',16,1,@return_value)
return @return_value
end
if(@irowCount = 0)
begin
print 'another transaction is trying to access the data'
set @counter += 1
rollback transaction
end
raiserror('Price updated',16,1)
commit transaction
set @counter = 0
return 0
end--end while loop
if(@counter = 3)
begin
raiserror('try again later',16,1)
return 99
end
【问题讨论】:
标签: sql-server tsql