2

I am creating a mysql statement that should update a table, but instead of setting a column name to be equal to some value the normal way, I decided to make it dynamic, for example instead of:

UPDATE mytable SET mycolumn = 'a value';

I use a variable. In this case:

SET @column = 'mycolumn';
UPDATE mytable SET @column = 'a value';

This returns a syntax error message when I execute the update statement. Is there a way to do this?

1 Answer 1

1

you can do this:

set @query = 'UPDATE mytable SET @column = ''a value''';
set @query = replace(@query, '@column', 'col_name');
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.