Begin Tran and Rollback

USE [tempdb]
GO

/****** Object:  Table [dbo].[Table_1]    Script Date: 02/06/16 18:05:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_1](
[fld1] [int] NULL,
[fld2] [int] NULL
) ON [PRIMARY]

GO



Insert into Table_1 (fld1,fld2) Values (0,100)


begin
Select * from Table_1
End


-- This will Terminat T-sql and rollback same if error occurs
begin Try

begin tran

Delete from Table_1 Where fld1 = 0
Insert into Table_1 (fld1,fld2) Values ('Switin',0)

COMMIT Tran

End Try

begin Catch
SELECT ERROR_MESSAGE() AS ErrorMessage;
ROLLBACK TRAN
End Catch




-- Begin End will not stop execution of next T-sql . Its Just logic Grouping of T-SQL
begin

Delete from Table_1 Where fld1 = 0
Insert into Table_1 (fld1,fld2) Values ('Switin',0)
Select * from Table_1
End



Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)