T-SQL Tuesday #21: Crap Code
I like to think that a good part of my job is helping coworkers fix their own crap code.Â But that doesn’t mean I’m not guilty of dropping a few bombs myself.Â (Granted, I had much more opportunity for that when I was a developer.)Â The hard part of this exercise is picking one instance.Â I mean, I could go with the code that didn’t work.Â Or I could go with the SSIS package that I originally wrote in DTS and used the wizard to convert.Â It all converted, technically, but I couldn’t migrate it from one environment to another without changing a bunch of connections or even adding linked servers to some of the code.Â I finally got around to rewriting that hot mess as a stored procedure that, believe it or not, runs in about 1/10th of the time.
But I think I’d rather talk about that damn Inventory program I’ve mentioned a couple of times already.Â Technically it’s not exactly T-SQL, but there was just so much suckage I really have to share.Â I wish I could show you how bad this thing was.Â First of all, it was SSIS. (I know!)Â Secondly, it was actually 3 different packages that all went and grabbed information from the same set of servers.Â On the same schedule.Â Why, you might ask, did I create 3 different packages?Â Was there, perhaps, a logical delineation between the 3?Â No.Â Not really.
You see, it started out as a single package that collected the job status info from all the servers, so that I could present it in a single report that was emailed to me daily.Â After a while, we determined there was a need for a central SQL Server inventory to track all our instances and the databases on them and that type of data.Â Now, please understand something.Â I’ve never taken any sort of class in SSIS or even read a book about it.Â What I “know” about SSIS is by Googling what I wanted to do and copying what someone else did.Â I didn’t actually understand what I was doing.Â So I didn’t exactly want to go fiddling around with my existing, working package.Â So I copied it.Â And I modified it to get the new information.
Then someone wanted to know all the logins that had sysadmin rights on all the instances.Â So I added that to the second package.Â Then they wanted linked server information.Â So I threw that in.Â Finally, they wanted to know all the objects that use linked servers.Â That one was pretty time intensive, so I made that a separate package.
But wait, it gets worse.Â I ask that you keep in mind that I created all this a LONG TIME AGO.Â Ahem.
- Some of the queries required elevated rights.Â Rather than figure out exactly what privileges I needed, I used the sa account to connect to each server.Â And because the sa account password varies, I had to hard code logic into the package for what password to use for which servers.
- I needed temp tables, but I couldn’t maintain the connection between different tasks in the package, so I had to create regular tables.Â I also didn’t know you could create regular user tables in tempdb.Â So I used the model database instead.
- I had no knowledge of error handling in SSIS, so I just had any failed task dump a record into a log table.Â This never worked correctly; I’d get the server name, but I could never get the actual error to log.Â I finally worked around the problem by setting the error_count threshold high enough that the job wouldn’t fail.
- I used sp_msforeachdb.Â A lot.Â (Come to think of it, maybe that should have been my topic today.Â Nah, this is much more cathartic!)Â I used a lot of cursors, too.
- I let this go on for YEARS.
Seriously, I didn’t want to mess with it at all.Â It felt like a house of cards.Â Push finally came to shove when one of the managers wanted to join back to this data, which meant it would have to move to Production.Â That’s when I decided it needed to move to 2008.Â And that’s also when I decided it needed to move out of SSIS.
I’ve since migrated it to Powershell.Â And, you know, it really didn’t take that long.Â I started exactly where I started all those years ago, with the job statuses, and once I got that working, adding the rest was easy.Â And omg is it fast!Â Sooo much faster than the SSIS packages.Â Granted, I left out the part about objects using linked servers.Â I have it written, but it’s going to be a separate, on-demand type thing.
Now, given that I know very little about Powershell, either, I’m sure some day I’ll look back at this new program and see it as a piece of crap, too.Â But for now, it’s all rainbows and unicorns.
P.S.Â I swear this is my last post bashing SSIS….Â Mainly because I don’t have anything else running in SSIS.