A
A
Alexander2016-11-02 10:30:34
MySQL
Alexander, 2016-11-02 10:30:34

How to pass mysql procedure argument to column name?

There is a procedure:

CREATE DEFINER = 'root'@'%'
PROCEDURE db.admin_change_product(
  IN infield varchar(255),
  IN invalue varchar(255),
  IN inidProduct int
  )
BEGIN
  UPDATE goods g SET g.infield = invalue WHERE g.id = inidProduct;
END

When executed, it gives:
Unknown column 'g.inField' in 'field list'
I tried to put ``:
UPDATE goods g SET `inField` = inValue WHERE `id` = inIdProduct;

The same.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-02
@Dredder

You are essentially trying to make a dynamic query (specify the names of columns, tables, etc. at runtime), but as you wrote it will not work. In MySQL, the only way to create dynamic queries is to collect them as strings and then execute them like this:

SET @myquery := CONCAT("UPDATE goods SET ", infield, " = ", invalue, " WHERE id = ", inidProduct);
PREPARE myquery FROM @myquery;
EXECUTE myquery;
DEALLOCATE PREPARE myquery;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question