Passing column name as a parameter in mysql stored function

后端 未结 3 1433
再見小時候
再見小時候 2021-01-14 05:11

I am using a MySQL stored function where I have created a function and I have passed column name as a parameter to this function. Suppose

CREATE DEFINER=`ro         


        
3条回答
  •  别那么骄傲
    2021-01-14 05:50

    I am not sure what you are trying to ask. But here is a solution using Prepared Statements:

    SET @selectStatement = CONCAT('Select ', columnName1,' from tab1 where id = 1'); -- Build Select statement like this
    PREPARE stmt FROM @selectStatement; -- parse and prepare insert statement from the above string 
    EXECUTE stmt; -- execute statement
    DEALLOCATE PREPARE stmt; -- release the statement memory.
    

    I don't know the complete scenario, but this may be avoided by changing some design.

提交回复
热议问题