I have the below
Declare @tbl Table(Id int identity, SomeCol varchar(10) not null)
Begin Transaction Tran1
Declare @ErrorNum int
Declare @i int
Set @i =1
--Start Operation
While(@i <= 10)
Begin
If(@i = 9)
Begin
Insert into @tbl(SomeCol) Values(null)
Set @ErrorNum = @@ERROR
End
Else
-- All records will be inserted successfully
Begin
Insert into @tbl(SomeCol) Values(@i)
End
Set @i = @i +1
End -- End of while
-- If there is any error, notify that and roll back the transaction
IF @ErrorNum <> 0
BEGIN
RAISERROR ('Attempt to insert null value in [Phone Number] is not allowed',16,1)
Rollback Transaction Tran1
End
IF (@ErrorNum = 0)
COMMIT TRANSACTION Tran1
select * from @tbl
What I am trying to do is that, if the value of @i is 9 , I am trying to insert a null value to the @tbl which should not allow at all and should rollback all the records and will generate only the custom exception.
But it is giving both system and custom exception and the records have been inserted and not rolledback except for the 9th record.
The below is what I got in the Message Tab
**(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 515, Level 16, State 2, Line 14
Cannot insert the value NULL into column 'SomeCol', table '@tbl'; column does not allow nulls. INSERT fails.
The statement has been terminated.
(1 row(s) affected)
Msg 50000, Level 16, State 1, Line 29
Attempt to insert null value in SomeCol is not allowed
(9 row(s) affected)**
And the below is in the Records tab
Id SomeCol
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
10 10
I am not sure what wrong I have done.
Help needed.
Table variables don’t get rolled back. Try using a
#temporarytable instead and your script should work as expected!