I agree with the concept of not putting business logic in SQL, as well as the concept of SQL, which is a set-based language.
Think of SOLID as a special OO implementation of some really good programming practices and apply them to any code you write, including SQL. A good table layout design will incorporate SOLID ideas.
I spent too much time debugging large complex stored procedures, so if you can avoid this, do it. If you must write stored procedure code, then SOLID can help you.
For example, I have a large stored procedure that queries dozens of tables and returns a dataset for use by the printer to send greetings . This is a classic example of a business process disguised as a “report” written in SQL.
Looking at this operation with a SOLID lens:
He should have a separate responsibility and the only reason for change.
The code determines who receives the letter, as well as what data falls into this letter. If any aspect of the process changes, you need to update and test the entire system. The solid principle is to have a function that determines who receives the letter and what is in it.
It can be as simple as having one request that returns a list of clients. Pass this as an array of identifiers or TVPs to another stored procedure to collect data.
Open / close
In most SP (stored procedure) procedures, the entire procedure is either hardcoded or “data driven”. Data is provided, as a rule, for working with SQL. The SQL flow is changed using CASE, discarding the value of a field or SQL in text fields that receive EVALd.
The SOLID approach to my problem can be seen in the “basically SP that we just created. Now I have an SP that calls GET_CUSTOMERS and another GET_DATA . This wizard basically controls the workflow and this workflow should not change. The wizard is closed for modification.
If we need to improve the system for sending letters, and emails have different data fields, I can change it to GET_DATA with a call to GET_DATA_FOR_EMAIL . Open for change.
The owner has one responsibility. If the workflow changes, then this wizard changes in accordance with rule No. 1.
Liskov substitution principle
This is very OO, but to create a point, we can say that GET_CUSTOMERS SP can be considered as a replaceable object here. I should be in another search, say GET_CUSTOMERS_WHO_NEED_DIFFERENT_LETTER and be able to reuse my code. Or maybe GET_CUSTOMERS_FOR_INTERNAL_TEST ? If all the components of your system are single-position and cannot be reused, the system will be more difficult to maintain and understand. It could be a stretch. Think less about overlaying OO objects and functions with similar arguments that can be reused.
Dependency Inversion Principle
This is the biggest mistake I've seen. Do not write code around selects and where clauses. When my system started up with a table, a table * SELECT * FROM, where customer.letter-sent = false * (actually more than 1 thousand lines), I hope that the search for all clients ends and send them an email. Start with abstractions and code, where the highest level controls the workflow in SP.
The idea is that the abstraction "finds customers" and returns a list of customers. I have to swap this logic for something else in the code. The violation of this, which I often see, is to "find clients" who write to the table connected to the client table, and then another SP that reads this table. In this case, your abstraction leaked into the implementation, and you switched from reusable code to a system that sends letters to a specific table.
SQL is very efficient, and it is too difficult to combine steps that must be separate in the abstractions that will be implemented in the implementation.
Stored procedures are a good tool that helps the reader work in a team to increase productivity and provides convenience through scalability. This is still code and theft of templates from other languages will only help if your overall design.
Very often you do not need a design template if you need a simple one. If your code is more than 50 lines or so, try moving it to the application level and / or think of solid re-factoring.