Site icon Andy M Mallon – AM²

sp_get_basetable_list

At work, we make extensive use of views & synonyms. The reasons are complex (and fodder for another post), but here’s a simplified example:

Pricing data is updated multiple times per day, and we want to keep multiple generations of the pricing table online. By pointing the synonym to the latest pricing table, we can redirect users to the current/correct data. If there’s a problem, we can “roll back” the latest pricing updates by simply pointing the synonym back to the prior generation of pricing data.

Obviously, this can create some confusion when you do need to find the actual table (eg, to see indexes, or to update data). What pricing table is the real pricing table?
Sure– you can query system views to find out the real table(s) referenced by the synonym or view.
When you do this hundreds (or thousands) of times, it can really add up. Time to automate.

First, let’s spec out what we’re going to automate:

Seems like a daunting list, but it actually turned out to be pretty simple. Let’s look at how we’ll accomplish all those things using SQL:

I was able to turn this into a stored procedure using less than 100 lines of code. Take a look at the code, and give it a shot for yourself. You can download it here.

This stored procedure has become the basis for a few other procedures I’ve created. Some of those are incredibly useful for me, but this is the cornerstone of that other code.

Exit mobile version