This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

How to use an insert using variable column names in stored procedure

I am sure that this happens because of my lack of knowledge, but I am in a dead end here. I am trying to pass a parameter from a stored procedure in order to use insert or update based on that parameter as a column name.

For example, I am trying to use this commands inside a stored procedure:

set @value = 'comment'; execute immediate concat('update log set ',@value,'= ?') USING 'lalalala';

However, when I use this concat inside a stored procedure, it just doesn't allow me, or in case of an insert doesn't do anything or makes the database freeze. What am I doing wrong? how can I make a stored procedure pass a column name as a parameter in order to use it on a successful insert/update?

I also tried to use the PREPARE / EXECUTE pair as well, but for no avail Please, any help would be welcome!

Answer Answered by Roshni K in this comment.

set @value = 'comment'; set @s1=concat("update log set ", @value,"=? "); execute immediate @s1 using 'lalalala';

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.