Nested transaction Rollback Example

Source: Internet
Author: User
Tags rollback savepoint
Reference: Introduction to SQL Transaction (Transaction) usage and rollback instance

--Test table
CREATE TABLE T1
	(
	ID int identity (1,1),
	Code nvarchar) go

--Stored procedure
Create Procedure P_rollbacktest 
as 
begin
	SET NOCOUNT on
	declare @n int = 0 
	
	begin tran Outtrans
	INSERT into T1 (code) VALUES (' A ')-		-Save in table	

	begin try 
		begin tran Innertrans INSERT INTO 

		T1 (code) VALUES (' B ')	--Before savepoint, save Tran SavePoint in the table-			-Create transaction save point 
		
		insert INTO T1 (Code) VALUES (' C ') After c22/>--savepoint, discard
				
		Set @n = 10/0				--Exception
		
		commit tran Innertrans end 
	try 
	begin catch 
		Rol Lback Tran SavePoint--rollback			to savepoint 
		Tran Innertrans			--Note: There is an error with rollback TRAN, only commit Tran end
	Catch 
	
	insert INTO T1 (Code) VALUES (' D ')-		-Save in table		
	commit tran Outtrans end	
go 

Exec p_ Rollbacktest
Go 

select * from T1 






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.