I’m looking to refactor the below query to something more readable and modifiable. The first half is identical to the second, with the exception of the database queried from (table names are the same though.)
SELECT Column 1 AS c1, ... Column N AS cN FROM database1.dbo.Table1 UNION SELECT 'Some String' as c1, ... NULL as cN FROM database1.dbo.Table2 UNION SELECT Column 1 AS c1, ... Column N AS cN FROM database2.dbo.Table1 UNION SELECT 'Some String' as c1, ... NULL as cN FROM database2.dbo.Table2
This query is the definition of DRY and is calling to me to be re-written, but I have no idea how!
EDIT: We can’t use linq and we desire distinct results; I’m looking to make the query smaller in physical file size, not in results returned.
EDIT: The database I’m querying off is a proprietary ERP database. Restructuring it is not an option.
This is a pretty standard SQL pattern. Sometimes it’s easy to inadvisedly transfer OOP/Procedural code principles like DRY to SQL, but they aren’t necessarily transferable concepts.
Note how easily you can grok the entire logical design of the query, vs. hunting through submodules. If one of the subexpressions had an extra column, or columns reversed, it would stick out. This is basically a pretty simple SQL statement to grok as an execution unit, where disaggregating it would muddle it.
And when you’re debugging, it’s handy to be able to use the editor’s text highlighting option to selectively exercise parts of the statement – a technique that doesn’t exist in procedural code. OTOH, it can get messy trying to trace down all the pieces if they are scattered into views etc. Even CTEs can make this inconvenient.