If it makes you feel any better Bob, the IF-ELSE cascade you mention is more than likely the 'right' way to do it.
SQL Server (specifically; I can't speak for MySQL or others) doesn't allow you to join against a table specified by a variable. You -can- join against a table variable, but that's a slightly different beast. You cannot join against a string variable containing the name of a table, however.
So, generally, you either write the join in a cascade of IF-THEN-ELSE bits, or you pre-fetch the data you need into a table variable and join against that. Which, when you can do it, is a nice way of being 'elegant'. But you can't always do that.
(Alternatively, if you want to call down the wrath of your fellow programmers and security experts, you can sneak around the rules by writing dynamic SQL -- self-generating code, basically -- but That Way Lies Madness, and most shops outright ban it these days.)
Anyway, sounds like you did good. Best of luck!
--sofaspud
--"Listening to your kid is the audio equivalent of a Salvador Dali painting, Spud." --OpMegs
SQL Server (specifically; I can't speak for MySQL or others) doesn't allow you to join against a table specified by a variable. You -can- join against a table variable, but that's a slightly different beast. You cannot join against a string variable containing the name of a table, however.
So, generally, you either write the join in a cascade of IF-THEN-ELSE bits, or you pre-fetch the data you need into a table variable and join against that. Which, when you can do it, is a nice way of being 'elegant'. But you can't always do that.
(Alternatively, if you want to call down the wrath of your fellow programmers and security experts, you can sneak around the rules by writing dynamic SQL -- self-generating code, basically -- but That Way Lies Madness, and most shops outright ban it these days.)
Anyway, sounds like you did good. Best of luck!
--sofaspud
--"Listening to your kid is the audio equivalent of a Salvador Dali painting, Spud." --OpMegs