gpt4 book ai didi

Mysql - 触发器中不允许使用动态 SQL

转载 作者:行者123 更新时间:2023-11-30 22:40:42 25 4
gpt4 key购买 nike

我想用动态 SQL 创建一个触发器,我从变量中获取列名。这是我简化的 mysql 模式:

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

即:

表格产品:

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

表属性值:

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

表产品属性:

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

在触发器中,我想在插入到表 attribute_values 后更新表 product_attributes。

当我执行查询时:

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

我得到错误:

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.

mysql有解决办法吗?

感谢您的努力:)

最佳答案

MySQL在函数和触发器中不支持动态插入但是程序可以支持动态插入。

所以你可以改变程序并在 attribute_values 表中创建过程插入查询。

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

然后你将只是调用过程

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 //

关于Mysql - 触发器中不允许使用动态 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31202072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com