Problems displaying this newsletter? View online.
SQL Server Central
Featured Contents
Question of the Day
The Voice of the DBA
 

Transactional DDL in SQL Server: In Oracle, It Does Not Compute

When you’ve spent your career working with Oracle, the idea that you can wrap a DDL statement in a transaction and then roll it back sounds like science fiction, but no, it’s real and I’m asked often by my Redgate Flyway team if Oracle can do the same as SQL Server, which does have this capability. Oracle treats most DDL as auto-committing, meaning once it executes, it's done. SQL Server, however, blurs that line in fascinating ways, allowing some DDL operations to be part of an explicit transaction.

Let’s explore how far that functionality goes and where the edges start to fray.

What SQL Server Can Roll Back with Transactional DDL

SQL Server supports transactional DDL to a surprising extent and for DevOps pipelines, it’s a feature put to good use. You can wrap many schema-changing operations in a BEGIN TRAN block and roll them back if needed. Here is an example:

BEGIN TRAN
    CREATE TABLE TestTable (ID INT);
    -- Other operations
ROLLBACK TRAN

The above will successfully roll back the table creation. That’s not just cosmetic, the DDL for the new table won't exist afterward.

Transactional DDL is supported for a large selection of DDL commands, including:

  • CREATE TABLE, DROP TABLE
  • CREATE PROCEDURE, DROP PROCEDURE
  • CREATE VIEW, DROP VIEW
  • ALTER TABLE (most column-level changes)
  • CREATE INDEX, DROP INDEX, ALTER INDEX

In these cases, you can treat DDL like DML, group it with other changes, test logic, and back out cleanly if needed.  This is an incredibly foreign concept to Oracle developers and DBAs, who would need a drop script to remove the object the DDL created.  There is no rollback unless you create one.

Limitations and Gotchas

While the capability is powerful, it's not universal or without caveats. Here's where SQL Server draws the line, as not all DDL is the same.  The DDL Statements Auto-Commited or can’t be rolled back, requiring the same type of undo scripts as Oracle include the following:

  • BACKUP, RESTORE
  • RECONFIGURE, SET SHOWPLAN, DBCC commands
  • ALTER DATABASE (e.g., adding files or modifying settings)
  • DROP DATABASE or CREATE DATABASE
  • TRUNCATE TABLE (not DDL per se, but it's auto-committing and can't be rolled back)

These commands either auto-commit or change things at a level that can't be safely encapsulated in a transaction.

Implicit Transactions vs. Explicit Transactions

When I first started out as a DBA, I was working in both Oracle and SQL Server.  I had a sign that was over my desk that said, “Commit or Be Committed” which was to remind me that in Oracle, while I need to commit my DML, in SQL Server, this was a given unless I explicitly chose to change the default behavior.  SQL Server can be configured with IMPLICIT_TRANSACTIONS ON, where even standalone DDL is wrapped in a transaction (until a commit/rollback is issued), but this behavior is session-specific and often disabled by default.

You must remember, if you're relying on rollback behavior, always use explicit BEGIN TRAN and remember to comment your code so other folks know what your code is about:

... ROLLBACK blocks in scripts to be sure.

One of the biggest surprises I discovered is that not all tools will result in transactional DDL responding the same.  In tools like SSMS (SQL Server Management Studio), some DDL behavior might appear transactional in the script window but not behave that way in a deployed change script, depending on how the engine and the GUI coordinate. This reminds us all how important it is for organizations to use proper release tools for deployments, (like Flyway) to eliminate inconsistent behaviors.  Always test your assumptions in the environment and mode you'll be using (interactive vs. deployment tool, etc.) no matter what you decide to do!

SQL Server uses a unified transaction log for both DML and (some) DDL operations. As long as the schema change doesn't require a full internal recompilation or system-level metadata update (like in ALTER DATABASE), the engine can keep track of it just like any other change, which makes rollback feasible.  This isn't the case in Oracle, where DDL creates implicit commits before and after execution, effectively sealing the operation and flushing the redo/undo buffers.  If you want to rollback any changes in Oracle, an undo script must be generated to remove the DDL change.

Practical Uses for Transactional DDL

If you're like me and working with customers on numerous DevOps pipelines, the topic of transactional DDL will come up, especially if they are multiplatform organizations.  The ability to have that "get out of jail free card" while doing a deployment can come in very handy.  If focused on releases, there are several benefits that I would add to the top of my list:

  • Safer migration scripts: Test schema changes as part of a controlled deployment.
  • Procedural rollbacks: Include CREATE or ALTER statements in logic that aborts on condition.
  • Versioning frameworks: Tools like Flyway or SSDT can benefit from rollbacks during dry runs or failed validations.

My Final Thought: Missing Out and Use with Caution

Just because SQL Server lets you roll back DDL doesn’t mean you always should. Some operations, especially those involving large tables or indexes, may still have performance implications or require exclusive locks. In mission-critical systems, rolling back DDL can lead to prolonged blocking or blocking chain effects.

And for cross-platform developers, never assume these behaviors translate to Oracle, PostgreSQL, or MySQL. Each engine has its own transaction model and limitations and differences abound.  If you’ve never used transactional DDL with SQL Server, test it out, it’s not hard to do and provides a unique look into a feature that simply doesn’t exist in the Oracle world.

BEGIN TRAN;
    CREATE TABLE Sample (ID INT);
    INSERT INTO Sample VALUES (1);
    SELECT * FROM Sample;  -- Works
ROLLBACK;
-- Now try to SELECT again (will fail if rollback worked)
SELECT * FROM Sample;  -- Error: Invalid object

Next time, maybe we’ll dig into the Oracle RECYCLE BIN and Flashback feature.  Yes, it's different, but the ability to travel in time with Oracle, is one I think you'll find interesting after we've touched on transactional DDL in SQL Server.

Peace out.

dbakevlar

Join the debate, and respond to today's editorial on the forums

 
 Featured Contents
SQLServerCentral Article

Monitor SQL Server using Zabbix

Pablo Echeverria from SQLServerCentral

This article explains the process to install the agents and configure the host to be monitored using Zabbix

SQLServerCentral Article

Database Architecture Considerations for Implementing Content Moderation Services

inbathiru from SQLServerCentral

Fellow SQL Server professionals, I wanted to share some insights and start a discussion about the database design challenges we face when implementing Content Moderation Services in enterprise applications. As more organizations build user-generated content platforms, the backend data architecture becomes critical for efficient moderation workflows. Database Design Challenges for Content Moderation Services Performance at […]

SQLServerCentral Article

Migrate from Linux to Windows with Availability Groups

Pablo Echeverria from SQLServerCentral

This article describes the process to create a read-scale cross-platform SQL Server Availability Group where the primary is Linux and the secondary is Windows.

Technical Article

TCP Provider Errors in SQL Server

dbakevlar from SQLServerCentral

 

The Unicorn Project

Site Owners from SQLServerCentral

In The Unicorn Project, we follow Maxine, a senior lead developer and architect, as she is exiled to the Phoenix Project, to the horror of her friends and colleagues, as punishment for contributing to a payroll outage. She tries to survive in what feels like a heartless and uncaring bureaucracy and to work within a system where no one can get anything done without endless committees, paperwork, and approvals.

 

 Question of the Day

Today's question (by dbakevlar):

 

TCP Provider Errors in SQL Server

You're troubleshooting a connectivity issue between a client application and a remote SQL Server instance. The client receives a "provider: TCP Provider, error: 0 - No such host is known" message. You verify that SQL Server is running and reachable on the server. The SQL Server instance is configured with a named instance and uses dynamic ports. Which of the following steps is most likely to resolve the issue?

Think you know the answer? Click here, and find out if you are right.

 

 

 Yesterday's Question of the Day (by dbakevlar)

Always On AG Latency for Read-Only Workload After Failover?

You're responsible for managing a SQL Server environment using Always On Availability Groups across two data centers. During a recent planned failover to the secondary replica, you noticed increased application latency and errors related to read-only routing. Upon investigation, you discover that read-only routing continues pointing to the former primary, now a secondary, for a period of time after the failover.
Which of the following best explains this behavior and how to resolve it?

Answer: The read-only routing list was not updated on the new primary; configure the routing list on all replicas to reflect the full failover topology.

Explanation: "The read-only routing list was not updated on the new primary; configure the routing list on all replicas to reflect the full failover topology." In an Always On Availability Group, the read-only routing list must be defined on each replica with its own perspective of the routing topology. If only the original primary had the routing configuration, a failover to a new primary means read-intent connections cannot be properly redirected, resulting in application errors or degraded performance. While MultiSubnetFailover and DNS TTL are relevant for listener connection and failover time, they do not govern read-intent routing behavior.

Discuss this question and answer on the forums

 

 

 

Database Pros Who Need Your Help

Here's a few of the new posts today on the forums. To see more, visit the forums.


SQL Server 2017 - Administration
Am I doing the right thing, and what does this mean? - On one of my database servers, I have a maintenance plan that includes the following tasks: 1. Check Database Integrity 2. Back Up Database Task 3. Recognize Index (includes Fragmentation > 15% & Page Count > 1000) 4. Update Statistics 5. Clean Up History (older than 4 weeks) 6. Maintenance Cleanup Task (1 week for […]
SQL Server 2016 - Administration
How to fix transactional replication latency - Good Morning All, We are facing replication latency issue. I checked for blocking, long running transactions or bulk operations running. I tried re-running the distribution agent job also. But still the latency exists. Could you please tell me what all I need to do to fix the latency Thanks, Chaitanya
SQL Server 2019 - Development
Upgraded VS2019 to version 16.11.49 but I cannot access my original SSIS package - Hi I upgraded to new version of VS2019 to 16.11.49.  My original SSIS package is not accessible anymore.  When I open my original package I get message "This tool window can only be used by a SQL Server Integration Services package document".  I am able to create new SSIS package so that tells me that […]
Editorials
How Microsoft Fabric Is Reshaping Data Value - Comments posted to this topic are about the item How Microsoft Fabric Is Reshaping Data Value
Life's Little Frustrations - Comments posted to this topic are about the item Life's Little Frustrations
Databases and Disasters - Comments posted to this topic are about the item Databases and Disasters
Article Discussions by Author
How a Legacy Logic Choked SQL Server in a 30-Year-Old Factory - Comments posted to this topic are about the item How a Legacy Logic Choked SQL Server in a 30-Year-Old Factory, which is is not currently available on the site.
We Gave Memory-Optimized Tables a Hash Lookup — Then Tried Pattern Matching Instead - Comments posted to this topic are about the item We Gave Memory-Optimized Tables a Hash Lookup — Then Tried Pattern Matching Instead
Always On AG Latency for Read-Only Workload After Failover? - Comments posted to this topic are about the item Always On AG Latency for Read-Only Workload After Failover?
Unlocking Interoperability: A Guide to Foreign Data Wrappers in PostgreSQL and Aurora PostgreSQL AWS RDS - Comments posted to this topic are about the item Unlocking Interoperability: A Guide to Foreign Data Wrappers in PostgreSQL and Aurora PostgreSQL AWS RDS
An Introduction To Database Diagrams - Comments posted to this topic are about the item An Introduction To Database Diagrams
When INCLUDE Columns Quietly Inflate Your Transaction Logs - Comments posted to this topic are about the item When INCLUDE Columns Quietly Inflate Your Transaction Logs
Automating SQL Server Access Review - Comments posted to this topic are about the item Automating SQL Server Access Review
SQL Server 2022 - Administration
Disaster Recovery for Azure SQL - I am creating a Disaster Recovery plan for Below Data pipeline. I need to plan for Azure SQL Data recovery and Partial Data recovery. May someone please help me with list what all I must consider while doing DR Plan for Azure SQL Data recovery and Partial Data recovery. Thanks a lot.    
SQL Server 2022 - Development
How do I make this one row per id - I've written this code, but it is showing right now as 2 rows per invg_id and there are alot of nulls, i would like to bring the elements of the sup and agent into 1 row per invg_id.  how would i rewrite this? select f.INVG_ID, case when f.COM_TYPE = 28 then f.MaxCmtInvgDt end as 'MaxAgentSFRDt', […]
 

 

RSS FeedTwitter

This email has been sent to {email}. To be removed from this list, please click here. If you have any problems leaving the list, please contact the webmaster@sqlservercentral.com. This newsletter was sent to you because you signed up at SQLServerCentral.com.
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved.
webmaster@sqlservercentral.com

 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -