用户登录
用户注册

分享至

SQL Server中TRUNCATE事务回滚操作方法

  • 作者: 举吊问天谁要吹
  • 来源: 51数据库
  • 2021-11-19

我们一般都认为truncate是一种不可回滚的操作,它会删除表中的所有数据以及重置identity列。

如果你在事务中进行truncate操作,就能回滚。反之,它就不会从日志文件文件恢复数据。它不会在日志文件中记录删除的那些数据,它只在日志中记录数据页的单元分配。

下面的例子就能解释上面的所说的.        

use temp_test_database
go
--创建一个临时表
create table truncatetabel(id int)
insert into truncatetabel(id)
select 1
union all
select 2
union all
select 3
go

--检查插入的数据
select * from truncatetabel

如图:

开始执行事务


--开始事务
begin tran
truncate table truncatetabel
go
--回滚之前检查truncatetable
select * from truncatetabel
go

f5执行,如图:

执行回滚事务

--回滚事务
rollback tran
go

再次检查表truncatetable

--回滚之后再次检查truncatetable
select * from truncatetabel
go

f5执行,如图:

总结一下,事务是可以对truncate操作进行回滚的。

软件
前端设计
程序设计
Java相关