On SQL and Reuse

time to read 2 min | 326 words

I’ve been thought that I should do my best to avoid duplicating code. And I’m pretty fanatic about this when I’m programming in a OO or procedural languages. But how do I do it with SQL? The issue keeps coming back with code that looks sort of like this:

 

INSERT INTO Actions (Code, NormalDescription)

SELECT 13, T.Description +’ ‘ +F.Description  FROM Feathers F, Tar T

WHERE  T.Amount > T.Amount;

 

INSERT INTO Actions (Code, CrimeDescription)

SELECT 14, T.Description +’ ‘ +F.Description  FROM Feathers F, Tar T

WHERE  T.Amount = T.Amount;

 

Now imagine that the statements are each 70 lines long, and including quite a bit of business logic. The difference between the two cannot be expressed (to my knowledge) in SQL. To the best of my knowledge, the only way to make this reusable is with dynamic SQL and string concentration (which are issues enough on their own.) I run into this a lot lately, the differences in the queries are miniscule, but insurmountable, so it seems.

Is there a good way to solve this?