Stumbled across the SSMS Tools Pack today. How has this been around since 2007, without me knowing about it? I installed it at work, and I’m already finding it useful.
Category: programming
found it
Following up on my post from a couple of days back, I found my invalid nulls today. There were null values where there shouldn’t have been on 20 records in a table of 100,000+. I fixed them. Tomorrow, I hope to zero in on whatever program bug is causing them to get in there in the first place.
A couple of quick observations:
First, SQL Profiler is my friend. There wasn’t really any way I could trace what the VB6 program was doing, and it wasn’t putting out enough error info for me to figure it out, but by following a SQL trace, I eventually isolated the problem records.
Second, I discovered Query ExPlus today, which turns out to be a pretty good tool if you need quick access to a SQL query tool on a machine where you can’t actually install the full SQL Management Studio.
how not to do error-handling
Currently working on a bug in an old VB6 system. The only thing we see in the logs is “Invalid use of Null”, which I know if the standard VB6 error you get when you try to access a value without checking to see if it’s null first. Since the system itself hasn’t changed in years, I know that, somewhere, in some table, there’s a null value where there shouldn’t be a null value. But, because the system doesn’t pass through the line # of the error, I can’t see which field it might be looking at. And, because it doesn’t pinpoint which record had the error, I can’t really tell which record it was processing when the error occurred. So, it could be any of about 100 different fields on any of about 100,000 different records. I’ve made an educated guess that it’s probably one of only a few different fields, and I think I’ve narrowed down the possible problem records too, but still, could I please ask anyone out there who may be writing non-trivial systems that may be in use long after they’ve left their company: If you’re going to “handle” exceptions, please do more than just eat them and report the error message! Please, for the love of god, at least report the line #. And, if you’re processing a large data set, give some indication of which record you were processing. Thank you.
PowerShell script to view SMTP server WMI stats
I’ve been playing with PowerShell a bit lately. Here’s a script I wrote today that extracts some info about the standard Windows Server SMTP service, does a little formatting on it, and sends it out to someone via GMail. (I’m sending it via GMail, since the purpose of the script is to determine if there’s anything weird going on with the SMTP service, and if there is, then it doesn’t make sense to use it to send the status e-mail.)
function sendmail
{
param ($msgtext)
$EmailFrom = "someone@somewhere.com"
$EmailTo = "someone@somewhere.com"
$Subject = "SMTP Stats"
$Body = $msgtext
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("somebody@gmail.com", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
$smtp1 = gwmi Win32_PerfFormattedData_NTFSDRV_SMTPNTFSStoreDriver | ? { $_.Name -eq '_Total' }
$smtp2 = gwmi Win32_PerfFormattedData_SMTPSVC_SMTPServer | ? { $_.Name -eq '_Total' }
$Date = Get-Date
$output = "-----------------------------------------------`n"
$output += "Stats from " + $smtp1.__SERVER + " on " + $date + "`n"
$output += "----------------------------------------------`n"
$output += "Messages in queue dir: " + $smtp1.Messagesinthequeuedirectory + "`n"
$output += "Remote queue length: " + $smtp2.RemoteQueueLength + "`n"
$output += "Remote retry queue length: " + $smtp2.RemoteRetryQueueLength + "`n"
$output += "`nBadmail:`n"
$output += "`tBadPickupFile: " + $smtp2.BadmailedMessagesBadPickupFile + "`n"
$output += "`tGeneralFailure: " + $smtp2.BadmailedMessagesGeneralFailure + "`n"
$output += "`tHopCountExceeded: " + $smtp2.BadmailedMessagesHopCountExceeded + "`n"
$output += "`tNDRofDSN: " + $smtp2.BadmailedMessagesNDRofDSN + "`n"
$output += "`tNoRecipients: " + $smtp2.BadmailedMessagesNoRecipients + "`n"
$output += "`tTriggeredviaEvent: " + $smtp2.BadmailedMessagesTriggeredviaEvent + "`n"
# $output
sendmail($output)
I still don’t really know PowerShell that well, but I’m learning. I picked up most of the info I needed to write this script from StackOverflow and Hey, Scripting Guy.
fun with stored procedures
I had some fun today optimizing a stored procedure. It was taking about 10 minutes to run, and I got it down to running in two seconds with a pretty minor change.
Without getting into too many gory details, it was basically doing a somewhat complicated PIVOT. It was using a SUM() call to generate a value that it was really just ignoring, just checking to see if it was null or not. I replaced the SUM() with a COUNT(), and checked for zero instead of null, and bam, 10 minutes down to 2 seconds.
This is the kind of stuff I really like doing — figuring out how to make a minor change in a bit of code that doesn’t affect the output, but results in a measurable improvement in performance. The only thing that’s still a little frustrating about this is that I don’t completely understand why the difference is so great. I have a general idea of why this worked, but something weird must be going on internally with the PIVOT option in MS SQL for this to have made such a huge difference.
less than helpful naming conventions
I had to do a little debugging work today in a system that I’m not too familiar with. I came across this line of code:
bool result = processor.Process();
If your code has reached the level of abstraction where the most meaningful name you can give a class is “Processor”, and the most meaningful name you can give the main routine in your class is “Process()”, then you may have gotten a little too abstract.
stored procedures
I had to debug a problem with a third-party e-commerce system today at work. It took a while to isolate the problem, and when I did, it led me to a certain stored procedure, which turned out to be 7400 lines long. I don’t think I’d ever seen a single stored proc quite that big before. Is that normal? I figured out what it was doing by running SQL Profiler, with the “TSQL_SPs” template that shows all the statements being executed within the procedure. With all the if/then logic in the proc, it was really only executing about 1000 lines of SQL for any given run. Still, that’s a lot to comb through just to figure out why the logic was returning wholesale prices instead of retail.
I also found a number of lengthy queries in the proc that ended with “and 1=2”, which would of course prevent that query from returning any results. I’m wondering why someone would do that rather than comment out the query. It made it fairly difficult to find the queries that were actually executing and returning results.
jQuery and CSS
I’ve been doing some client-side stuff at work recently, and I’m realizing that I don’t know nearly enough about some of this stuff. So, I picked up a book on jQuery, “jQuery: Novice to Ninja”, and another one on CSS, “CSS Mastery: Advanced Web Standards Solutions”. The jQuery book is pretty good, and a pretty easy read. I haven’t started the CSS book yet. I’m somewhat chagrined to admit that I’ve kind of been “faking it” with CSS for the last few years. I’ve never actually sat down and read a book on CSS or taken a class, or anything like that. I know enough to get by, usually, but I definitely need to learn more.
Meanwhile, I’ve put off my attempt to get through any more ASP.NET exams for now. I’m just letting work needs dictate my independent study time. At some point, hopefully, I’ll be up to speed on enough of the stuff at work that I can get back to the certification exams.
Oh, and as an example of how little I know about CSS, I really don’t know how to style a div so these two Amazon link boxes will line up horizontally instead of vertically. If I messed around with it for an hour, I’d probably figure it out. But I should probably know how to do something like that off the top of my head.
a fun project at work
I’m starting a project at work right now that’s going to involve integrating content from a WordPress blog and Picasa Web into an ASP.NET site. I’m a little leery about this, since it seems that this could all be done directly in .Net, and would be more efficient that way, but I’m game to give it a try. It should be fun. (Yeah, I know, now that I’ve said that, I’m doomed.)
ASP.NET and other .NET stuff
I spent some time today watching a couple of dnrTV episodes on Entity Framework. (Episodes 117 and 118.) I may be using Entity Framework in the near future.
Meanwhile, my progress through the book for 70-562 has stalled at chapter 6. I got through chapters 1-5 in about a week, but then last week, I got distracted from this due to a sudden burst of job interviews (three in a row). I also just wanted to take my time getting through the ASP.NET AJAX material, since I found it to be very interesting, and I wasn’t that familiar with it.
I bought a cheap used copy of Dino Esposito’s “Introducing Microsoft ASP.NET AJAX” book from Amazon, to supplement the overview in the 70-562 book. It looks like this will be a good place to start. I’m not sure now when I’ll be getting back to studying for 70-562, though, due to these side trips. I’d still like to take the test next month, but we’ll see if I have the time to do that or not.