简体   繁体   English

如果在SQL Server中回滚事务,它会删除临时表(在事务中创建)吗?

[英]does it drop temp table (created in transaction) if rollback transaction in sql server?

In the transaction, I created some temp tables and inserted some data. 在事务中,我创建了一些临时表并插入了一些数据。 if everything goes ok, the temp tables will be dropped and then the transaction will be committed. 如果一切正常,将删除临时表,然后提交事务。 if there is exception, the connection will be close. 如果有异常,连接将关闭。

My question is: Do I have to rollback the transaction before close the connection? 我的问题是:我必须在关闭连接之前回滚事务吗? or, as long as the connection is closed, it is ok? 或者,只要关闭连接就可以了吗?

If I rollback, I assume the temp tables created in transaction will be dropped (rollback) too, correct? 如果回滚,我假设在事务中创建的临时表也将被删除(回滚),对吗?

thanks 谢谢

A temp table will be dropped when it's out of scope. 临时表超出范围时将被删除。

However losing a connection while having a transaction open creates an orphaned transaction, and any locks it holds will remain. 但是,在打开事务时丢失连接会创建一个孤立的事务,并且它持有的任何锁都将保留。

Using SET XACT_ABORT should rollback any transaction as soon as a run-time error is encountered. 一旦遇到运行时错误,使用SET XACT_ABORT应该回滚任何事务。

http://msdn.microsoft.com/en-us/library/ms188792.aspx http://msdn.microsoft.com/en-us/library/ms188792.aspx

The temp table will drop as soon as it is out of scope. 临时表超出范围后将立即删除。 Closing the connection puts it out of scope regardless of whether or not it rolls back. 不管连接是否回滚,关闭连接都会使其超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM