I need to create some stored function in MySQL that will work exactly like getter/setter and return or set for me variable by it's name.
For example get_variable('my_special') will return me value of @my_special variable, set_variable('my_special', 23) will set @my_special := 23.
Question is: how can I, having variable name as a string set or get it in MySQL?
Smth like SET @{'my_special'} = 23 or $$var = 23 (as in PHP)
Update: According to my task I found that it's impossible to do in mysql. The purpose I wanted this was a chain of events:
- I wanted to store query with variables as a view in DB. MySQL rejects storing view with variables but allows with using functions.
- I've decided I'll create functions which will return/set my variables. But I had about 4 variables inside query - so it's not efficient to create 4 pairs of functions to get/set variables. So I've wanted to create universal getters/setters.
- Only way to get/set variable by name is to run dynamic query which are forbidden inside functions(only in procedures which are not very comfortable to use inside select statements).
- So as a result of this question - it's impossible.