Mysql - dynamic SQL not allowed in trigger

六眼飞鱼酱① 提交于 2021-02-10 20:35:29

问题


I want to create a trigger with dynamic SQL where I get a column name from a variable. Here is my simplified mysql schema:

CREATE TABLE products (id int);
INSERT INTO products VALUES (1),(2);

CREATE TABLE attribute_values 
(product_id int, `key` varchar(100), value varchar(100));
INSERT INTO attribute_values VALUES
( 1, 'title', 'Orange'),
( 1, 'code', 'O125'),      
( 2, 'title', 'Pizza');


CREATE TABLE product_attributes 
SELECT  products.id,
        MAX(CASE WHEN attribute_values.key = 'title' THEN attribute_values.value END) title,
        MAX(CASE WHEN attribute_values.key = 'code' THEN attribute_values.value END) code
FROM    products JOIN attribute_values ON products.id = attribute_values.product_id
GROUP   BY products.id;


# trigger
DELIMITER //
CREATE PROCEDURE attribute_values_after_insert(IN product_id INT, IN column_name VARCHAR(100), IN val VARCHAR(100))
BEGIN
    SET @sql = NULL;
    SELECT concat('UPDATE product_attributes SET product_attributes.', column_name, '=', val, ' WHERE id=', product_id) INTO @sql;

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    SET @sql = NULL;
END//
DELIMITER ;

DELIMITER //
CREATE TRIGGER attribute_values_insert_trigger AFTER INSERT ON attribute_values FOR EACH ROW
BEGIN
    CALL attribute_values_after_insert(NEW.product_id, NEW.key, NEW.value);
END
DELIMITER ;

http://sqlfiddle.com/#!9/674fd/1

i.e:

table products:

+-------------+
| id          |
+-------------+
| 1           |
| 2           |
+-------------+

table attribute_values:

+-------------+----------+-----------+
| product_id  | key      | value     |
+-------------+----------+-----------+
| 1           | title    | Orange    |
| 1           | code     | O125      |
| 2           | title    | Pizza     |
+-------------+----------+-----------+

table product_attributes:

+-------------+----------+-----------+
| id          | title    | code      |
+-------------+----------+-----------+
| 1           | Orange   | 0125      |
| 2           | Pizza    |           |
+-------------+----------+-----------+

In the trigger I want to update table product_attributes after inserting to table attribute_values.

When I execute query:

INSERT INTO attribute_values VALUES (2, 'code', '0126');

I get error:

DDL and DML statements are not allowed in the query panel for MySQL; only SELECT statements are allowed. Put DDL and DML in the schema panel.

Is there a solution for this in mysql?

Thanks for any efforts :)


回答1:


MYSQL IN FUNCTION AND TRIGGER NOT SUPPORT DYNAMIC INSERT BUT PROCEDURE CAN SUPPORT DYNAMIC INSERT.

SO YOU CAN MAKE CHANGES IN PROCEDURE AND MAKE PROCEDURE IN attribute_values TABLE INSERT QUERY.

INSERT INTO attribute_values VALUES(product_id,column_name,val);

AND AFTER YOU WILL BE JUST CALL PROCEDURE

CALL attribute_values_after_insert(2,'code',132);

DELIMITER //
drop procedure if exists attribute_values_after_insert //
CREATE PROCEDURE attribute_values_after_insert(IN product_id INT, IN 
column_name VARCHAR(100), IN val VARCHAR(100))
BEGIN
SET @sql = NULL;

INSERT INTO attribute_values VALUES(product_id,column_name,val);

SELECT concat('UPDATE product_attributes SET product_attributes.', column_name, '=', val, ' WHERE id=', product_id) INTO @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = NULL;
END//
DELIMITER ;

DELIMITER //


来源:https://stackoverflow.com/questions/31202072/mysql-dynamic-sql-not-allowed-in-trigger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!