Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Does 'DROP TABLE' inside transaction causes an implicit commit?

My question is kind of easy but i'm still doubting after I created this transaction. If I execute the following code:

BEGIN TRANSACTION       DROP TABLE Table_Name 

Can I perform a ROLLBACK TRANSACTION that recovers the dropped table? I'm asking because I don't know what happens in the 'Object Explorer' and I didn't found any question of this topic, so I think that it could be a useful issue.

like image 347
Mauro Bilotti Avatar asked Jul 18 '14 14:07

Mauro Bilotti


People also ask

Does DROP TABLE commit?

Like the CREATE statement, the DROP statement automatically executes a COMMIT before and after dropping the table. This makes permanent all changes to the database since the last COMMIT or ROLLBACK.

What will happen when the following statement is executed DROP TABLE Table_name?

It completely removes the table structure and associated indexes, statistics, permissions, triggers and constraints. You might have SQL Views and Stored procedures referencing to the SQL table. SQL Server does not remove these stored procedures and views.

Do we need to commit after DROP index?

After wasting a lot of time on non-solutions to the problem, I found the real solution is to commit immediately after dropping the index. When commit is issued, the table is unlocked and the insert transactions complete successfully.


1 Answers

This is incredibly easy to test.

create table TransactionTest (     ID int identity primary key clustered,     SomeValue varchar(20) )  insert TransactionTest select 'Here is a row'  begin transaction     drop table TransactionTest rollback transaction  select * from TransactionTest 
like image 124
Sean Lange Avatar answered Oct 01 '22 21:10

Sean Lange