ALTER TABLE DROP COLUMN in Azure SQL Database: Uncertain Rollback Safety
0 reputation · 26 Jan 2024, 14:25 UTC
0 reputation · 26 Jan 2024, 14:25 UTC
Determine the exact transactional boundaries for schema changes in Azure SQL Database, specifically focusing on operations that are auto‑committed and cannot be rolled back, and explore whether any configuration can enforce rollback safety.
ALTER TABLE DROP COLUMN, are auto‑committed regardless of the surrounding transaction.The documentation does not clearly define which DDL operations are auto‑committed versus fully transactional, leaving developers uncertain about predictable rollback behavior.
34775 reputation · 26 Jan 2024, 23:40 UTC
ALTER TABLE DROP COLUMN in Azure SQL Database is transactional and can be rolled back when executed inside an explicit BEGIN TRANSACTION. It is not auto-committed by default. The confusion comes from other RDBMS engines where DDL is auto-committed, and from Azure SQL Database operations that are truly auto-committed at the database level.
The premise that DROP COLUMN is auto-committed is likely carried over from MySQL or from Azure SQL Database database-level operations. The engine does auto-commit a small set of database-scoped changes that cannot be rolled back in a user transaction, e.g., CREATE DATABASE, DROP DATABASE, ALTER DATABASE, and some system catalog operations. Table-level ALTER TABLE DROP COLUMN is not in that set.
BEGIN TRANSACTION;
ALTER TABLE dbo.YourTable DROP COLUMN YourColumn;
-- verify with SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.YourTable')
ROLLBACK; -- or COMMIT;
Rollback is only possible if the statement succeeds inside the transaction. If the column is referenced by an index, foreign key, primary key, computed column, or an indexed view, the ALTER will fail before the metadata change and the transaction will be aborted. Rolling back large schema changes can generate significant log activity and undo time.
One missing diagnostic detail that changes the recommendation: are there dependent objects on the column, such as indexes, constraints, or indexed views? If yes, the drop must be preceded by dropping those dependencies, otherwise the transaction will fail and rollback will be moot.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.