SQL 在事务中截断表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1522931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:54:07  来源:igfitidea点击:

Truncate Table Within Transaction

sqlsql-serversql-server-2005transactionstruncate

提问by Davin Studer

Can the SQL "truncate table" command be used within a transaction? I am creating an app and my table has a ton of records. I want to delete all the records, but if the app fails I was to rollback my transaction. Deleting each record takes a very long time. I'm wondering if I use truncate table, can I still rollback the transaction and get my data back in the event of a failure. I realize that truncate table doesn't write each delete to the transaction log, but I'm wondering if it writes the page deallocation to the log so that rollback works.

可以在事务中使用 SQL“truncate table”命令吗?我正在创建一个应用程序,我的表有大量记录。我想删除所有记录,但如果应用程序失败,我将回滚我的交易。删除每条记录需要很长时间。我想知道如果我使用 truncate table,我是否仍然可以回滚事务并在发生故障时取回我的数据。我意识到截断表不会将每个删除写入事务日志,但我想知道它是否将页面释放写入日志以便回滚工作。

采纳答案by womp

In SQL Server, you can rollback a TRUNCATE from a transaction. It does write page deallocation to the log, as you mentioned.

在 SQL Server 中,您可以从事务中回滚 TRUNCATE。正如您所提到的,它确实将页面释放写入日志。

回答by Jonathan Leffler

In Oracle, TRUNCATE TABLE is a DDL statement that cannot be used in a transaction (or, more accurately, cannot be rolled back). AFAIK, if there is a transaction in progress when the statement is executed, the transaction is committed and then the TRUNCATE is executed and cannot be undone.

在 Oracle 中,TRUNCATE TABLE 是不能在事务中使用的 DDL 语句(或者更准确地说,不能回滚)。AFAIK,如果在执行语句时有正在进行的事务,则提交事务,然后执行 TRUNCATE 并且无法撤消。

In Informix, the behaviour of TRUNCATE is slightly different; you can use TRUNCATE in a transaction, but the only statements permissible after that are COMMIT and ROLLBACK.

在 Informix 中,TRUNCATE 的行为略有不同;您可以在事务中使用 TRUNCATE,但之后唯一允许的语句是 COMMIT 和 ROLLBACK。

Other DBMS probably have their own idiosyncratic interpretations of the behaviour of TRUNCATE TABLE.

其他 DBMS 可能对 TRUNCATE TABLE 的行为有自己的特殊解释。