Situation: A PHP application with multiple installable modules creates a new table in database for each, in the style of mod_A, mod_B, mod_C etc. Each has the column section_id.
Now, I am looking for all entries for a specific section_id, and I’m hoping there’s another way besides ‘Select * from mod_a, mod_b, mod_c … mod_xyzzy where section_id=value’… or even worse, using a separate query for each module.
If the tables are changing over time, you can inline code gen your solution in an SP (pseudo code – you’ll have to fill in):
— or this
I’ve used this technique occasionally, when there just isn’t any obvious way to make it future-proof without dynamic SQL.
Note: In your loop, you can use the COALESCE/NULL propagation trick and leave the string as NULL before the loop, but it’s not as clear if you are unfamiliar with the idiom: