Error Handling and Transaction Control

TRY/CATCH, XACT_ABORT, rollback behavior, and transaction outcomes.

19 associated posts18 primary posts

T-SQL & Development

Can You Nest Transactions in SQL Server?

To find out, let's set up a simple status log table:
[crayon-6a6eb81b6de22460642823/]
And then let's try a two-part transaction:
[crayon-6a6eb81b6de2b353364948/]
Right now, SQL Server shows that I have 2 open transactions:

What Happens If I Roll Back?
But what does "2 open transactions" mean, really? If I do a rollback, what gets rolled back? Let's find out:
[crayon-6a6eb81b6de2d836696877/]
The results:

Read more about Can You Nest Transactions in SQL Server? 5 comments — Join the discussion
T-SQL & Development

[Video] Fundamentals of Stored Procedures at SQLBits

Anybody can write a stored procedure with a little help from Google. This session is about how to write stored procedures that have a high likelihood of performing well and are easy to troubleshoot.

This fast-paced, all-demo session from SQLBits will NOT cover how to write a query, syntax, or performance tuning. This is about good best practices after you've written the first one - things like how to catch errors, how to pass in multiple values, how to debug without the debugger, and more.

Read more about [Video] Fundamentals of Stored Procedures at SQLBits 12 comments — Join the discussion
T-SQL & Development

Error Handling Quiz Week: Where Are You Handling Errors?

This week's series on error handling has been an eye opener for many of us. We've laughed. We've cried. We've screamed in horror. We've read the documentation. I don't blame you. This is a really confusing topic, and for many of us, it's the first time we've actually looked at the mechanics of how transactions,…

Read more about Error Handling Quiz Week: Where Are You Handling Errors? 11 comments — Join the discussion
T-SQL & Development

Error Handling Quiz Week: Making a Turkey Sandwich with XACT_ABORT

CAN YOU BELIEVE THAT HOT MESS IN YESTERDAY'S POST?!?

I know, right? You thought that by combining try/catch with a transaction, you'd get robust error handling.  Instead, you ended up with half the tables populated, and a leftover open transaction. You'd already forgotten Tuesday's post in which I pointed out that TRY/CATCH doesn't catch low severity or high severity errors.

Read more about Error Handling Quiz Week: Making a Turkey Sandwich with XACT_ABORT 31 comments — Join the discussion
T-SQL & Development

Error Handling Quiz Week: Combining Transactions And TRY/CATCH

In yesterday's epic cliffhanger, you might have been shocked to discover that a plain ol' transaction does not solve our problem - and in fact, it makes things worse. With yesterday's code, we still got rows inserted into the Parent table, no rows in the Child table, and a nasty surprise that our session will have to deal with down the road.

Read more about Error Handling Quiz Week: Combining Transactions And TRY/CATCH 18 comments — Join the discussion
T-SQL & Development

Error Handling Quiz Week: Tryin’ TRY/CATCH

Let's say we have two tables, Parent and Child, and we need to guarantee that they both get populated at once. We'll write a single stored procedure to do both inserts: [crayon-6a6eb81b6f137572901030/] I put a WAITFOR in there, but that isn't the problem - I'm just using that to demonstrate why the code isn't production-ready.…

Read more about Error Handling Quiz Week: Tryin’ TRY/CATCH 34 comments — Join the discussion
T-SQL & Development

How to Batch Updates A Few Thousand Rows at a Time

You've got a staging table with millions of rows, and you want to join that over to a production table and update the contents. However, when you try to do it all in one big statement, you end up with lock escalation, large transaction log usage, slow replication to Availability Groups, and angry users with pitchforks gathered in the Zoom lobby.

Read more about How to Batch Updates A Few Thousand Rows at a Time 17 comments — Join the discussion
T-SQL & Development

How to fix the error “String or binary data would be truncated”

To fix this error, patch to SQL Server 2016 SP2, CU6 or newer (including SQL Server 2017), and then turn on trace flag 460. You can enable it at the query level or at the server level.

First, let's see the error happen: let's create a table with small fields, and then try to insert more data than it holds.
[crayon-6a6eb81b70fcc962447831/]
Baby's car is longer than 20 characters, so when the insert statement runs, we get an error:
[crayon-6a6eb81b70fd4889574488/]

Read more about How to fix the error “String or binary data would be truncated” 82 comments — Join the discussion
T-SQL & Development

What TRY/CATCH Doesn’t Handle

We were once asked in class what TRY/CATCH doesn't handle besides object existence errors.

It's well documented in Books Online (BOL). If you're like me, then tl;dr. Are we even calling it Books Online these days? I still say "bookmark lookup" instead of "key lookup". I suppose I'll be saying Books Online for quite some time too. At least these days it really is online.

Read more about What TRY/CATCH Doesn’t Handle 25 comments — Join the discussion