Here's my code. It's a very simple: the 1st INSERT
fails while the last two succeed.
Before I go on, I want to clarify that I understand that I can plug in an IF statement
with @@ERROR
or BEGIN ROLLBACK
that will solve the issue. I know how the issue can be solved.
Since both INSERTs in the script are between a BEGIN/COMMIT TRAN
, I thought that this would be considered a single transaction. So in my case, since the 1st INSERT
fails, then I assumed that the following INSERT
s wouldn't executed or wouldn't be committed. It seems that's not the case.
Is the purpose of BEGIN TRAN
and COMMIT TRAN
to be able to use a the @@ERROR
function and/or ROLLBACK TRAN
between them?
IF OBJECT_ID('tempdb..#testTable') IS NOT NULL DROP TABLE #testTable
create table #testTable
(
Id int not null,
Name varchar(100)
)
BEGIN TRAN T1;
insert into #testTable select null, 'Joe';
-- select @@ERROR
if @@ERROR = 0
BEGIN
insert into #testTable select 1, 'Bob';
insert into #testTable select 2, 'Ralph';
END
COMMIT TRAN T1;
select * From #testTable
Thanks.