我正在嘗試使用事務編寫一個sproc。任何人都可以告訴我,下面的代碼是否會有問題,或者它是否能按預期工作?第一次嘗試進行sproc事務
ALTER procedure [dbo].[DeleteMetricMeter]
(
@SectionID int,
@MetricMeterID int,
@Result bit output
)
as
declare @MetricMeterCount int
declare @err int
declare @rowcount int
set xact_abort on
begin tran
select @MetricMeterCount = count(*) from luMetricMeters
where fkSectionID = @SectionID
select @err = @@error, @rowcount = @@rowcount
if (@err <> 0) or (@rowcount = 0)
begin
goto on_error
end
delete from luMetricMeterList
where pkMetricMeterID = @MetricMeterID
select @err = @@error, @rowcount = @@rowcount
if (@err <> 0) or (@rowcount = 0)
begin
goto on_error
end
delete from luMetricMeters
where pkMetricMeterID = @MetricMeterID
select @err = @@error, @rowcount = @@rowcount
if (@err <> 0) or (@rowcount = 0)
begin
goto on_error
end
if (@MetricMeterCount = 1)
begin
delete from luMetricSections
where pkSectionID = @SectionID
select @err = @@error, @rowcount = @@rowcount
if (@err <> 0) or (@rowcount = 0)
begin
goto on_error
end
end
commit tran
set @result = 1
return @result
on_error:
rollback tran
set @result = 0
return @result
您正在使用哪個版本的SQL Server? – 2011-02-18 09:19:30