2011-02-14 66 views
2

我有以下回滾沒有發生......爲什麼?

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 

我不知道自己做錯了什麼我做了。

需要幫助。

回答

2

表變量不會回滾。嘗試使用#temporary表,而您的腳本應該按預期工作!

create Table #tbl(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) 
BEGIN 
    PRINT 'COMMIT' 
    COMMIT TRANSACTION Tran1 
END 
    select * from #tbl 

drop table #tbl 
+0

這很好..我現在用#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

+0

@user。不知道在2000年是否/如何抑制這種情況。以後的版本可以使用`TRY ... CATCH` – 2011-02-14 13:48:27

0

看起來,你是永遠不會 Rollback Transaction Tran1 - 因爲RAISE之前出現。 只是交換兩條線

0

如果您添加以下腳本,那麼整個交易將失敗(否部分加載表變量)回滾的開頭,但你可能看不到您的自定義錯誤消息:

SET XACT_ABORT ON 

從微軟documentation

當SET XACT_ABORT爲OFF時,在某些情況下, 只引發錯誤的Transact-SQL語句 回滾 和交易繼續 處理。根據錯誤的嚴重程度 ,當SET XACT_ABORT爲OFF時,整個 事務可能會回退,即使在 也是如此。 OFF是默認設置 。

如果你想成爲rolledback併發布你的自定義錯誤訊息支付,你可以使用的try ... catch交易:

BEGIN TRY 
    Declare @tbl Table(Id int identity, SomeCol varchar(10) not null) 

    Begin Transaction Tran1 
     Declare @i int 
     Set @i =1 

     --Start Operation 
     While(@i <= 10) 
     Begin 

      If(@i = 9) 
       Begin 
        Insert into @tbl(SomeCol) Values(null) 
       End 
      Else 
       -- All records will be inserted successfully 
       Begin 
        Insert into @tbl(SomeCol) Values(@i) 
       End 
      Set @i = @i +1 

     End -- End of while 

     COMMIT TRANSACTION Tran1 

     select * from @tbl 
END TRY 
BEGIN CATCH 
     BEGIN 
      RAISERROR ('Attempt to insert null value in [Phone Number] is not allowed',16,1) 
      Rollback Transaction Tran1 
     End 
END CATCH 
相關問題