DECLARE @TranStarted int
DECLARE @ErrorCodeint
--开始事务
IF( @@TRANCOUNT = 0 )
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
--处理查询内容
--查询结束
--发生错误转向错误处理
IF( @@ERROR <> 0 )
BEGIN
SET @ErrorCode = -1
GOTO Cleanup
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
--提交
COMMIT TRANSACTION
END
RETURN(0)
Cleanup: --错误处理
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
--回滚
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
DECLARE @ErrorCodeint
--开始事务
IF( @@TRANCOUNT = 0 )
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
--处理查询内容
--查询结束
--发生错误转向错误处理
IF( @@ERROR <> 0 )
BEGIN
SET @ErrorCode = -1
GOTO Cleanup
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
--提交
COMMIT TRANSACTION
END
RETURN(0)
Cleanup: --错误处理
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
--回滚
ROLLBACK TRANSACTION
END
RETURN @ErrorCode