I have a scenario:
I have like 5 Boolean fields in a table, I need to set an option set value based on the number of true values of those 5 Boolean Fields:
Field1
Field2
Field3
Field4
Field5
ENUMField1 values (A,B,C,D,E)
If I have 5 of those Boolean fields as True, I will set the ENUM field value as A,
4 Trues and I set them as B
and so on
As I may need to change the logic of those values in the future , I need to set/change them on DB side on save ,rather than on controller.
Thanks
As I’m not familiar with stored procedures I couldn’t manage to find a solution.
3
Answers
A stored procedure is just putting your SQL on the server rather than calling it from the client. There’s nothing special about it, and it isn’t a way to write logic to run on the server (there are ways to do this, varies on programming language, but the DB is not recommended as a place to store your application code)
You create a SP with the following syntax. Use your own SQL in the middle.
how you call that varies by your client language, but typically its little different than calling a parameterised SQL statement.
Assuming your enum runs from A-Z then using ascii conversions where dec 65 is A
eg
drop table if exists t;
If you have more and more columns then you will need dynamic sql.
Maybe generated column is more suitable? It will provide actual value always, rather than stored procedure which must be executed each time when you need in actual values..
DEMO
fiddle