我有以下回滾沒有發生......爲什麼?
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
我所試圖做的是,如果@i的值是9,我試圖插入一個空值的@tbl這不應該讓所有和應該回滾所有記錄並只會生成自定義異常。
但它給出了系統和自定義異常,並且記錄已被插入,而不是回滾,除了第9條記錄。
下面是我在郵件選項卡
**(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)**
GOT和下面是在記錄片
Id SomeCol
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
10 10
我不知道自己做錯了什麼我做了。
需要幫助。
這很好..我現在用#temp表。它的工作。但我只想生成自定義消息。我得到(1 row(s)affected) .... (1 Msg 515,Level 16,State 2,Line 14 不能將值NULL插入'SomeCol'列'tempdb.dbo。#tbl'__________________________________________________________________________ 00000000000C ';列不允許爲空,INSERT失敗 該語句已被終止。 (1行(一個或多個)受影響) 消息50000,級別16,狀態1,行30 嘗試中插入空值[電話號碼]不允許 (影響0行(S)) – user1 2011-02-14 13:43:56
@user。不知道在2000年是否/如何抑制這種情況。以後的版本可以使用`TRY ... CATCH` – 2011-02-14 13:48:27