T-SQL Tuesday #24: Prox ‘n’ Funx

Procedures and Functions. Well, this could be interesting. Everyone else’s posts, that is.

T-SQL Tuesday

#24, brought to us by Brad Schulz

OK, T-SQL Tuesday Twenty-Four: Two-year anniversary edition! Pretty sweet of Adam Machanic (blog | @AdamMachanic) to come up with this whole thing two years ago. A good guy, he is. This month’s topic is Prox ‘n’ Funx, brought to all of us by Brad Schulz (blog). I kind of feel bad here—I don’t really know much about Brad, but he’s an MVP, and flipping around his blog brings up some pretty cool posts. I really like this one, for example.

I actually have what I think is a decent little thing to talk about for this topic. It could have been a stretch topic for T-SQL Tuesday #11 about misconceptions, even if [I hope] not exactly a widespread one. This topic revolves around transaction control and error handling within stored procedures, which can be a crazy topic in and of itself. Books could be written on the topic. Specifically, the conversation that I found myself involved in one day was about what happens to any open explicit transactions when a procedure runs into an error.

Let’s Talk About Procs Dying

Once upon a time, someone said to me that if an error occurs within a procedure which contains an explicit transaction, that transaction will remain open, potentially blocking other sessions on the system. In short, that’s not true, and it’s fairly easy to prove. We’ll work through a quick little script to do this, and will include modern TRY…CATCH error handling, because that will come in when we get to the main point (I say “modern” there like it’s a new thing, but coming from a guy who has worked with SQL 2000 more than anything else, that distinction feels necessary). It actually doesn’t matter if there is any error handling during the first part of this exercise, as the results will be the same. This is a pretty contrived setup, but that’s pretty much what I’m going for.

First, create a table & put a couple of rows into it. The two columns we’re most worried about are “FakeNumberColumn” and “ActualNumberColumn” (note their data types), so named because the error which will be triggered in a little bit will be a type conversion error.
-- Create Table
CREATE TABLE dbo.TransactionTest2008
      
(     TransactionTestID       INT                     NOT NULL    IDENTITY(1,1),
            
RowDescription          VARCHAR(50)             NOT NULL,
            
FakeNumberColumn        VARCHAR(10)             NOT NULL,
            
ActualNumberColumn      INT                     NULL
      )
;

-- Populate it with a couple of rows
INSERT INTO dbo.TransactionTest2008 (RowDescription, FakeNumberColumn)
      
SELECT 'Actually a Number 1', 10
            
UNION ALL
      
SELECT 'Actually a Number 2', 100
;

Now for the really contrived part: a Stored Procedure that will throw an error if FakeNumberColumn contains something that won’t implicitly convert to a numeric:

CREATE PROCEDURE TransactionTester2008
AS

BEGIN try
  
  
BEGIN TRAN
     UPDATE
dbo.TransactionTest2008
          
SET ActualNumberColumn = FakeNumberColumn * 2
     
--   select *
-- from dbo.TransactionTest2008

     -- Wait for 10 seconds so we have a chance to look at DBCC OPENTRAN (unless of course it dies first)
     WAITFOR delay '00:00:10'

   COMMIT TRAN
END
try

BEGIN catch
-- Some kind of error has occured
  
PRINT 'Welcome to Catchville'

   IF @@TRANCOUNT > 0
      
ROLLBACK

   -- Raise an error with the details of the exception
  
DECLARE @ErrMsg NVARCHAR(4000), @ErrSeverity INT
   SELECT
@ErrMsg = ERROR_MESSAGE(),
      
@ErrSeverity = ERROR_SEVERITY()

   RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH


(The commented-out SELECT statement can be un-commented if you like, to see the state of the table at that point of execution.)

As it is now, the proc will run successfully. The 10 second WAITFOR in it gives you time to run DBCC OPENTRAN in another query window to see the proc’s open transaction.

EXEC TransactionTester2008

Open TransactionNow we’ll make things somewhat interesting. Insert another row into our table to put a letter into FakeNumberColumn, then run the proc again.

INSERT INTO dbo.TransactionTest2008 (RowDescription, FakeNumberColumn)
     
SELECT 'Not really a Number', 'F'

EXEC TransactionTester2008

Things won’t go so well this time…

Bombed Proc RunWe get the PRINT message about being in Catchville, so we know that our exception was caught and execution finished in the CATCH block. At this point, go run DBCC OPENTRAN again, and you will see that there isn’t a transaction open. This would be the expected behavior. No transactions are left open; the in-process activities are rolled back.

I should also note that a less-severe error, such as a constraint violation on an INSERT, will only cause an error in that particular statement. The Engine will skip over that statement & continue processing normally. That behavior has led to some near-brown pants moments while running a huge pile of INSERTs, putting in some provided business data, but that’s what explicit transactions are for!

Now, About Timeouts…

OK, that section wound up pretty long. Here’s where I’m actually getting to what I want to talk about…

We’ve established that errors in Stored Procedures will not lead to transactions being left open under normal circumstances. There is a situation where things don’t go so well: when a client/application connection times out for one reason or another. If this happens, the client side will close its end of the connection, and after the in-progress query SQL Server is running completes, nothing else really happens. This can leave open transactions, which, of course, are bad, bad, bad.

Starting with where we left off above, we can simulate an application timeout by cancelling the running SP in Management Studio.

First, delete the error-producing row from dbo.TransactionTest2008:

DELETE FROM dbo.TransactionTest2008
  
WHERE RowDescription = 'Not really a Number'

Execute TransactionTester2008 again, and this time, while in the 10-second WAITFOR, cancel the query in Management Studio. Even with the TRY…CATCH block in place, the explicit transaction is left open (check with DBCC OPENTRAN). What this means is that whatever application (or DBA!) running the statement(s) is responsible for closing an open transaction if a session times out or is cancelled. In my experience, if one is in the habit of wrapping everything you do in explicit BEGIN/COMMIT/ROLLBACK TRAN, they’ll be less likely to cancel a script they’re running and then sit there blocking half of the rest of the world. Not that I’ve been there, or anything…

There is a safety net here: XACT_ABORT. I first learned about XACT_ABORT while doing some work with Linked Servers a few years ago. What XACT_ABORT does when set to ON is to force SQL Server to terminate and roll back the entire batch if any error occurs. Here’s the Books On Line page for XACT_ABORT.

In our case, flipping that setting to ON within our test SP will change what happens when a query timeout (or cancel) happens. Add “SET XACT_ABORT ON” at the begging of the above SP and re-create it thusly (either drop and recreate or add the line and change it to ALTER PROCEDURE):

CREATE PROCEDURE TransactionTester2008
AS

SET XACT_ABORT ON
[…]

Run the SP as before, and again, while in the 10-second WAITFOR, cancel the query. Now if checking for an open transaction, there won’t be one—it was rolled back by the engine when the timeout (cancel) occurred, because of XACT_ABORT. No locks are still being held, no other sessions will be blocked by the timed-out session. Smooth sailing 🙂

As Usual, Be Careful

Before hauling off and adding this to a bunch of SPs, beware the Law of Unintended Consequences. Test extensively, because no matter how self-contained a fix may seem, who knows what else may be affected. This is no where this is as true as it is in old, overly-complicated, poorly-understood legacy systems. I know DBAs like to fix problems and prevent problems from happening in the first place, but please make sure that no new issues are introduced while trying to “fix” things.

Oh; don’t forget to clean up after yourself!

DROP TABLE dbo.TransactionTest2008
DROP PROCEDURE TransactionTester2008

 

Meme Monday: #SQLFamily

It’s Meme Monday. This is LaRock’s (blog | @SQLRockstar) brainchild. Its idea is to spur all of us to write something, and my sister agrees that this is a good idea. So, here I am, writing about this month’s topic, which is: what #sqlfamily means to me.

So, what does #sqlfamily mean to me? I love these guys. All of them. Even the ones that don’t follow me back on Twitter and/or think I’m a total muppet (for the record, I don’t care at all if you don’t follow me on Twitter. Alternatively, if you’re not on Twitter at all, I think you’re broken, but that’s a different story). I would say that I love these guys like family, but that would be pretty obvious.

See, the deal is, I’m not a big family type of guy. I don’t know why that is. My family is just as crazy as everyone else’s; nothing special there. Maybe it’s the part about how everyone just expects you to show up for things just because it’s a family function. If that is the case, one of the reasons that #sqlfamily is better is because nobody expects that. We all have lives outside of any one particular thing that we do, so for each thing, there’s always other stuff that we could be doing. We’re all the same in just the right ways, so everyone understands.

That’s another thing—we’re all the same in just the right ways. Tammy and I basically can’t talk about what we do with our family and a lot of our friends, because they just don’t get it. We can’t be like, “yeah, so the business today wanted this report deployed with no filtering in place—all 20,000 rows of it,” because nobody will really get it. Sure, the SQL Community is pretty heavily-weighted towards the Engine side of things, and we’re mostly BI people these days, but pretty much everyone understands at least a little bit. So, what does #sqlfamily get us? People who understand.

Most everyone knows by now (well…umm…) about the wedding at PASS. Like we’ve said before, that was pretty nerdy, pretty over-the-top, and more than a little weird. That description fits us pretty well, so I thought it worked out alright. You know who else fits that description pretty well? Yeah, a lot of #sqlfamily. And guess what? These people understand.

Hopefully you’re getting the theme. That’s what SQL Family means to me: People, friends, family who understand.

There are lots of days where that’s all I am looking for. Thanks, guys <3

SQL PASS Summit Day…Uhhh, All the Rest

Since I couldn’t keep up with writing every day (or nearly so) weeks ago at the Summit, recap post(s) weren’t going to go up until after the fact. Since my life has been pretty crazy since getting home (combined with some semi-minor problems with the web host), this isn’t getting finished until way late. I honestly don’t know where everyone found the time to write in the middle of all of this, because I flat-out need more sleep than that. I’m only grabbing the highest of the highlights here, and I’m doing this the weeks after! Normally I would say that I’ll try to do better next year, but I’m pretty sure that’s not going to happen.

Wednesday

Washington State Convention Center

Yep, those are overhead electrical lines for pantograph-equipped busses

Wednesday started out with a Keynote address, as most conferences do. This one had a few announcements, such as SQL Server v.Next getting a real name replacing its Codename. Instead of “Denali”, we now have “SQL Server 2012.” Nobody should be overly surprised by this, unless you were really hoping for 2011. The technical content of the keynote was very heavily focused on end-user flavors of BI technologies. Some of the stuff we’d seen before, but there were some new bits here and there. Generally, the “managed self-serve BI” space looks pretty similar to what it did before, being based around PowerPivot and other similar tools in Excel, like tabular data stores (VertiPaq). I thought it was an awful froofy* presentation, but most Keynote-type presos are. What was shown on screen was fine and all, and was neat to look at, but as I’ve said before, the hard part about “managed self-serve BI” is the “managed” part. There’s a lot of work that still needs to happen on the back end by people who know what they’re doing with respect to both the Business and the Data in order to get to the point where a C-level manager is poking around in Excel and making good, data-backed decisions. On that note, though, how many business users have the technical know-how to do the work that needs to be done in Excel to get there? And how many of those are C-level managers? OK, that’s a different post for a different day.

The main sessions on Wednesday for me were pretty heavy SSAS & related topics (like Data Mining). I had a big lightbulb kick on in Craig Utley’s SSAS Aggregations session confirming something that I had been suspecting, so the whole thing got off to a really good start for me. I can’t work on our aggregations situation right this second, but it’s something that will be making it onto my list for the mid-term after some current project work gets finished up.

Some of the days at PASS Summit have special wardrobe…themes. For the last couple years, there has been #sqlkilt day. This particular theme occurs on the same day as the Women in Technology lunch/panel discussion. Its purpose is to “support women in technology” by the guys wearing skirt-like apparatus. It’s cool, but a kilt guy I am not. Anyway, Wednesday this year was SQL Saturday day, where one was to wear their favorite SQL Saturday shirt. Since I’m not a speaker (yet?), all I have is my t-shirt from Nashvegas’s SQL Saturday last year. (It bothered me to wear a t-shirt on a conference day at first, but I got over it by lunch or so.) Coming out of the keynote I looked for what shirts everyone had on. Honestly, a non-zero part of this was me looking for who else just had t-shirts on, but I also wanted to see what everyone else was wearing. Turns out: There weren’t many! I wish I knew why this was. I suppose it could be due to not hearing about it…But I’m a little afraid that it’s because not that many have SQL Saturday shirts! People!! Free training by high-quality speakers (ie, not me). I’m sure there’s one near you. Website right here. Check it out.

The Couch

For my Purdue peeps, think Mathews Hall's ground-floor Men's room

Wednesday night was the vendor reception & some extra-curricular activities afterwards. We had planned to go to two things, but at one point, we were standing on the expo floor when we realized the first thing had started about a half hour ago. We started back to the hotel (which, due to when we started to schedule this trip, wasn’t close), when we realized we weren’t going to be able to get everywhere we wanted to when we wanted to. So, scrapped the first event, and got some food before heading to SQLKaraoke. I don’t know how much of that really needs to be discussed, as it’s fairly self-explanatory. Suffice to say that I fulfilled what had become a lifetime goal in recent years, tried to make friends/be nice to the cranky local guy sitting at the end of the bar, and we got home at 0300 again.

Thursday

Thursday… yeah, that keynote didn’t happen. Haven’t had a chance to go back and watch it yet, either.

More BI-related sessions for me, as that’s what I do now. I did go to one Engine session the whole time, and it was Aaron Bertrand’s (blog | @AaronBertrand) “T-SQL: Bad Habits to Kick” on Thursday. I was looking to see what kinds of things I do that are bad, because, honestly, T-SQL is not my strongest suit. Unfortunately, there wasn’t anything major that I didn’t already know, but I did get some background as to the “why”s on a few things. Plus, the only reason “what happens when you don’t declare a length on a varchar() variable” wasn’t news is because we ran into that in an old store

d proc at the end of September. I did learn about TRY_CONVERT() in SQL 2012, though. Although at first blush, it sounds pretty good, it also looks like it could be a big crutch. OH, and also I’m glad that I’m not the only one who finds non-ANSI 92-style joins hard to read at this point. Alright, so obviously I got more out of that session than I initially thought.

Thursday night had an interesting occurrence or two. I mean, there was this thing (someday maybe we’ll tell my parents, haha). I’m pretty happy about that. Yeah, it was an over-the-top nerdy thing to do, and more than a little weird, but that fits us pretty well, so I’m OK with it. Some people did make the chilly trek out to Puget Sound with us, and I/we are very, very grateful to everyone that did. We <3 our SQL Family.

We went to the official second-to-last night Gameworks party for a little bit, but were pretty worn out. We tried to hang on, but once again turned in early. Tammy was passed out on the couch within about three minutes of walking in the door, and I didn’t last a whole lot longer. I think I did try to write something that night, but realized quickly that if I wanted to keep drool off my work laptop, sleep was in order. Plus, I knew we had to get up early on Friday, because it’s Friday, and…

Friday…Dewitt Keynote!

See, for at least the last couple years (that’s as far back as I know, because I didn’t pay close attention to the conference in 2009, as I was bitter about not going), the last day of the conference’s Keynote has been a presentation by Dr. David DeWitt. This has yet to disappoint. This year, the topic was Big Data, including discussions about Relational DBs vs NoSQL DBs. I learned a lot from this, and think I understand why Microsoft is going in the direction that they are when it comes to NoSQL & related technologies. It will be fun to see where this goes. Dr. DeWitt’s keynote is available on-demand here (along with the others), but they do require minor registration. His slide deck is available here.

I would be remiss if I didn’t bring your attention to this little thing that happened at the beginning of the Friday Keynote. The song was hilarious, but I’m still pretty sure that Buck Woody with a 12-string is > *

More BI sessions on Friday, of course, along with a Professional Development one. The PD session was a panel discussion entitled, “Are you a Linchpin? Career management lessons to help you become indispensible.” The discussion was led by Andy Leonard (blog | @AndyLeonard). Panel members were: Andy Warren (blog | @SQLAndy), Stacia Misner (blog | @StaciaMisner), Louis Davidson (blog | @drsql), Brent Ozar (blog | @BrentO), Thomas LaRock (blog | @SQLRockstar), Kevin Kline (blog | @kekline), and Jeremiah Peschka (blog | @peschkaj). While writing this post, I listed everyone out, then almost deleted this whole paragraph to start over, because I felt like this was the biggest name drop bomb in the history of blogging. I then decided to leave it, because I can use that blob of names to make a point: Every single one of them has a blog and Twitter account. One could argue about an incorrect causation/correlation conclusion, but I think it’s telling that some of the most successful people in SQL Server Land use these communication methods heavily. I sometimes goof around on Twitter more than I use it constructively, and only like four people read my blog regularly, but if you’re a SQL Server DBA/Dev/etc and enjoy helping people and rubbing elbows with others that do, there’s no time like the present to start using these mediums if you don’t already. There are plenty of people here already who will support you and help you grow and learn.

Conference centers always start the tear-down ASAP at the end of the last day. I understand this—there’s a hard number of hours between when one event ends and the next one begins. It always makes me sad, though. Nothing says an event is over like the registration desk getting torn down in front of you. The good news for us on Friday afternoon while this was happening is that we had spent a lot of time with the awesome community that we’re a part of, and there were still good times ahead, both Friday night and at future events. More on that later, though.

To be continued…

I will cover some of the weekend happenings in another post soon, but more importantly, I have a few lessons learned and/or overall comments to make about the PASS Summit. I’m not going to get that written right now and this post is already too long and has taken me too many days to finally get together, so I’m going to split that up and get to it very soon. Thanks for reading so far…

* As usual, I reserve the right to make up my own words, as this is my blog.

SQL PASS Summit Logical Days 1 & 2

Logical Days? Well, the Conference doesn’t /technically/ start until Wednesday (tomorrow, as I’m writing this), but, 1) we’re here, and 2) I really do think of Pre-Cons and other organized functions as part of the conference itself, so these days counted for me.

Monday

Our conference trip started at 1:30 PM Central Daylight on Monday. With pushback scheduled for 4:45, this was wayyy out of whack for my usual scheduling. Because of how long it had been since we had flown, I didn’t know what security would look like at BNA or how long the process itself would take these days. Turns out, there have been little changes in the duration of things, so we wound up downstream of Checkpoint Charlie almost 2 hours before preboarding would start. Damn.

Anyway, ~5 hours later, we were in the back of a black Tahoe being driven by a Russian dude. I didn’t arrange the ground transportation; I was merely a pawn in a greater game passenger.

One of THE places to hang out & see people at, other than the Sheraton Lobby, of course, is the Tap House (it plays music, I’m sorry). We went there for supper, along with a couple of Tammy’s coworkers that are here. They have 160 beers on tap, and I had a few different Porters throughout the night. I was able to meet a good chunk of SQL Celebrities for the first time, and it’s always kind of fun to meet people like this. Twitter’s really good at making this happen—you feel like you’ve known these people for half your life, yet you’ve never actually met face to face. This happens a lot. In fact, because of my whole picture situation (I’ve got the Ray Bans on in all of the close-ups), Jorge Segarra (blog | @SQLChicken) was beginning to think that I was just a well-written bot. I thought that was pretty funny. For all you may know, he’s right.

Then there was the Mladen (blog | @MladenPrajdic) stuff, but I think that’s under NDA.

Long story short, we finally got back to the hotel at 0300 Tuesday morning. We’re off to a good start.

Tuesday

DowntownTransitTunnel
Seattle Transit Tunnel

Tuesday was mostly uneventful. We had an errand to run in the morning, which involved the usual public transportation crash course that happens in a new city. Turns out Seattle has this cool underground tunnel system for busses in the central downtown area. It’s just like the subway, except the bus routes run through there when they’re in the area. It’s a bit weird & pretty cool.

Tonight there were a few official events. The first was a First-Timers orientation & mixer session. We decided late to not go to that, instead going straight to the Welcome Reception/Quiz Bowl. The Quiz Bowl is where pairs (or three) SQL Server “Experts” compete in a Jeopardy!™-style competition where they basically are subject to the whims of Tim Ford (blog | @SQLAgentMan). Many LOLs ensue. After that was the SQL Server Central/Red Gate Exceptional DBA Presentation/Party. Got to meet Steve Jones (blog | @Way0utWest) finally, along with lots of other community members.

Of course, there is more SQL Karaoke going on tonight, but we decided that after the night we had last night, and the nights that we will have for the rest of the week, we should take it easy once.  Just about to pass out for the night, to be rested for tomorrow and all of the crazy learning (and just crazy) that will be going on for the next few days.

I’m here now, and I still can’t wait for whatever is going to happen next #sqlpass

PASS Summit From Home

I know that I have at least a couple people who read this (incidentally, I just realized they have the same first name) who might be interested in watching some of the Keynotes from PASS Summit next week, but aren’t plugged in to the SQL Server Community to notice this on their own 😉

The best place to find out what is being streamed online to watch off-site is going to be Jen’s post here on “attending PASS Summit Virtually.” It’s pretty much where I was going to go, because I know she’s got everything listed in one place that I can get right to, so you guys had just as well go there directly. Plus, it’s a fun post to read, so it’s still worth a couple minutes.

Even if Keynotes aren’t your thing (I understand), at least check out Dr Dewitt’s session on Friday morning. I was able to catch bits & pieces of his last year, and if you think that you know a little bit about databases, this is sure to destroy any self-confidence you may have in knowing what you are doing. It’s awesome.

So, that’s pretty much all I’ve got here. Check out Jen’s page & catch what you can and/or are interested in.