This month's T-SQL Tuesday is about lessons we've learned the hard way. Which, of course, is the way you learn best. It's not the best way to learn, but if you've suffered in your learning somewhat, then you're probably going to remember it better. Big thanks to Raul Gonzalez (@sqldoubleg) for dragging up these memories.
Oh, I could list all kinds of times I've learned things the hard way, in almost every part of my life. But let's stick to SQL.
There was a guy who needed to get his timesheets in. It wasn't me – I just thought I could help …by making a copy of his timesheets in a separate table, so that he could prepare them there instead of having to use the clunky Access form. I'd gone into the shared Access file that people were using, made a copy of it, and then proceeded to clear out all the data that wasn't about him, so that he could get his data ready. I figured once he was done, I'd just drop his data in amongst everyone else's – and that would be okay.
Except that right after I'd cleared out everyone else's data, everyone else started to complain that their data wasn't there.
Heart-rate increased. I checked that I was using the copy, not the original… I closed it, opened the original, and saw that sure enough, only his data was there. Everyone else's (including my own) data was gone.
And then it dawned on me – these tables were linked back to SQL in the back end. I'd copied the reference, but it was still pointing at the same place. All that data I'd deleted was gone from the actual table. I walked over to the boss and apologised. Luckily there was a recent backup, but I was still feeling pretty ordinary.
These kinds of problems can hurt in all kinds of situations, even if you're not using Access as a front-end. Other applications, views within SQL, Linked Servers, linked reports – plenty of things contain references rather than the actual thing. When you delete something, or change something, or whatever, you had better be sure that you're working in the right environment.
I don't even know the best way to have confidence that you're safe on this. You can help by colouring Prod tabs differently in SSMS with SSMS Tools Pack, but it's not going to guarantee that you're okay. You need to be a little paranoid about it. Learn to check and double-check. Because ultimately, data is too valuable to make that kind of mistake.