I'd like to be able to use variable column names in a stored function. Let me give you a simple example:
Now instead of all the IF/ELSEIF sections I'd like to use only one query where I could put the 'param' variable in place of the field name. From what I have researched so far it is possible to use prepared statements as a workaround - but this works only in stored prodecured and not functions. Is there any way to achieve the same result in functions?Code:CREATE FUNCTION `SOME_FUN`(`param` CHAR(30), `param_id` INT) RETURNS TEXT READS SQL DATA BEGIN DECLARE val TEXT; IF param='name' THEN SET val = (SELECT name FROM table WHERE id=param_id); ELSEIF param='price' THEN SET val = (SELECT price FROM table WHERE id=param_id); ELSEIF param='address' THEN SET val = (SELECT address FROM table WHERE id=param_id); ELSEIF param='code' THEN SET val = (SELECT code FROM table WHERE id=param_id); END IF; RETURN val; END
Of course, the above example is just a simple function to represent the idea, the function I want to use is much more complex.




Bookmarks