4 ms·
There was a time that doing exactly this (fixing, updating, or modifying existing queries) was a significant chunk of my day-to-day work. It certainly can be pa
by oarabbus_ 4y ago
There was a time that doing exactly this (fixing, updating, or modifying existing queries) was a significant chunk of my day-to-day work. It certainly can be painful and tedious, but that's quite universal - certainly not specific to SQL.
Unlike "true" programming languages (not to get into a debate about SQL's completeness, we all know it isn't practically a general-purpose language) this is usually best approached differently than debugging a program. Whenever I encountered a mess of a query, I'd ask myself two questions:
1. What is the purpose of the query/what is the desired output?
2. What are the source tables used in the query?
At this point I'd typically rewrite the query, taking inspiration from the non-broken parts of the original if possible, and otherwise rewriting from scratch. In most cases this was faster (and the rewritten result often more efficient) than trying to debug a truly busted statement.