我正在创建一个触发器,该触发器调用必须检查新数据的函数。我想将新行作为参数传递给此函数:
CREATE TRIGGER t BEFORE INSERT ON a FOR EACH ROW
BEGIN
CALL func(NEW);
END
我该怎么做?谢谢
答案 0 :(得分:0)
也许您可以使用新数据类型ROW做一些事情:
MariaDB [_]> SELECT VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 10.3.9-MariaDB-1:10.3.9 |
+-------------------------+
1 row in set (0.000 sec)
MariaDB [_]> DROP FUNCTION IF EXISTS `func`;
Query OK, 0 rows affected (0.001 sec)
MariaDB [_]> DROP TABLE IF EXISTS `a`;
Query OK, 0 rows affected (0.002 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `a` (
-> `col0` INT NOT NULL,
-> `col1` INT NOT NULL
-> );
Query OK, 0 rows affected (0.006 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE TRIGGER `t` BEFORE INSERT ON `a`
-> FOR EACH ROW
-> BEGIN
-> DECLARE `result` BOOL DEFAULT FALSE;
-> DECLARE `NEW_ROW` ROW (`_col0` INT);
->
-> -- SET `result` := `func`(NEW);
-> SET `NEW_ROW`.`_col0` = NEW.`col0`;
-> SET `result` := `func`(`NEW_ROW`);
->
-> IF (`result`) THEN
-> SET NEW.`col1` := 1000;
-> END IF;
-> END//
Query OK, 0 rows affected (0.001 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> CREATE FUNCTION `func` (`NEW_ROW` ROW (`_col0` INT))
-> RETURNS BOOL
-> DETERMINISTIC
-> RETURN `NEW_ROW`.`_col0` > 10;
Query OK, 0 rows affected (0.000 sec)
MariaDB [_]> INSERT INTO `a`
-> (`col0`, `col1`)
-> VALUES
-> (1, 1),
-> (11, 1);
Query OK, 2 rows affected (0.007 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [_]> SELECT `col0`, `col1`
-> FROM `a`;
+------+------+
| col0 | col1 |
+------+------+
| 1 | 1 |
| 11 | 1000 |
+------+------+
2 rows in set (0.002 sec)