回滚根本没有发生...为什么?
我有以下内容
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)**
下面是在“记录”选项卡中得到的内容
Id SomeCol
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
10 10
我不确定我做错了什么。
需要帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
表变量不会回滚。尝试使用
#temporary
表,您的脚本应该按预期工作!Table variables don't get rolled back. Try using a
#temporary
table instead and your script should work as expected!看起来你永远无法到达
回滚事务 Tran1
- 因为 RAISE 之前出现过。只需交换两行即可
It looks like that you are never get to
Rollback Transaction Tran1
- since RAISE appears before.Just swap both lines
表变量无法回滚。 请参阅此处。
Table variables cannot be rollbacked. See here.
如果将以下内容添加到脚本的开头,则整个事务将在失败时回滚(没有部分加载的表变量),但您可能看不到自定义错误消息:
来自 Microsoft 文档:
如果您希望回滚事务并发出自定义错误消息,您可以使用 Try...Catch:
If you add the following to the begining of your script then the entire transaction will be rolled back on failure (no partially loaded table variable), but you might not see your custom error message:
From the Microsoft documentation:
If you'd like the transaction to be rolledback and to issue your custom error messasge, you could use Try...Catch: