我正在尝试使用游标来处理包含字符串的行:
CREATE PROCEDURE REVERT_ALL(IN TABLE_NAME VARCHAR(255))
BEGIN
DECLARE bDone INT;
DECLARE CH_ID INT;
DECLARE CH_CHANGE CHAR;
DECLARE curs cursor for
SELECT `table_id`, `change_type` FROM mysql_snapshot.db_changes where `table_name` = "rooms";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
OPEN curs;
insert into splog set text = concat('SELECT id, `table_name`, table_id, `change_type` FROM mysql_snapshot.db_changes where table_name = ',TABLE_NAME, ';');
SET bDone = 0;
REPEAT
FETCH curs INTO CH_ID, CH_CHANGE;
insert into splog set text = concat_ws( ' -- ', 'CH_ID ', CH_ID,' TABLE ',TABLE_NAME, ' CH_CHANGE ', CH_CHANGE);
UNTIL bDone END REPEAT;
CLOSE curs;
END;
由于我正在搜索大部分时间的原因,
where `table_name` = "rooms"
似乎被忽略了。计划是将其改为
where `table_name` = TABLE_NAME
使用procedure参数。我只是获得所有行。插件用于记录和调试。
答案 0 :(得分:1)
您在过程参数名称TABLE_NAME
和表列名table_name
之间存在歧义。应该避免它,因为它会导致像这样的模糊问题。
在这种情况下,在该过程中,反引号中的TABLE_NAME
,table_name
和table_name
都是interpreted as the local variable name(参数名称)。所以,你的条件'where table_name = ',TABLE_NAME
总是如此;当您使用TABLE_NAME
文字替换"rooms"
时,如果您使用"rooms"
作为参数调用过程,则条件始终为true,否则始终为false。考虑这个简化的例子:
DROP PROCEDURE IF EXISTS pr;
DROP TABLE IF EXISTS t;
CREATE PROCEDURE pr (IN TABLE_NAME VARCHAR(255))
SELECT `id`, `table_name`, TABLE_NAME FROM t
;
CREATE TABLE t (id INT, `table_name` VARCHAR(255));
INSERT INTO t VALUES (1,'hotels'),(2,'rooms');
CALL pr("rooms");
CALL pr("foo");
你会得到
MariaDB [test]> CALL pr("rooms");
+------+--------------+------------+
| id | `table_name` | TABLE_NAME |
+------+--------------+------------+
| 1 | rooms | rooms |
| 2 | rooms | rooms |
+------+--------------+------------+
2 rows in set (0.00 sec)
MariaDB [test]> CALL pr("foo");
+------+--------------+------------+
| id | `table_name` | TABLE_NAME |
+------+--------------+------------+
| 1 | foo | foo |
| 2 | foo | foo |
+------+--------------+------------+
2 rows in set (0.00 sec)
如您所见,无论参数如何,对于所有行,table_name
始终等于TABLE_NAME
。
现在,如果您使用显式TABLE_NAME
替换"rooms"
:
DROP PROCEDURE IF EXISTS pr;
DROP TABLE IF EXISTS t;
CREATE PROCEDURE pr (IN TABLE_NAME VARCHAR(255))
SELECT `id`, `table_name`, "rooms" FROM t
;
CREATE TABLE t (id INT, `table_name` VARCHAR(255));
INSERT INTO t VALUES (1,'hotels'),(2,'rooms');
CALL pr("rooms");
CALL pr("foo");
现在,对于所有行,条件table_name = "rooms"
对于所有行都是正确的,对于第二次调用,对于所有行,条件MariaDB [test]> CALL pr("rooms");
+------+--------------+-------+
| id | `table_name` | rooms |
+------+--------------+-------+
| 1 | rooms | rooms |
| 2 | rooms | rooms |
+------+--------------+-------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> CALL pr("foo");
+------+--------------+-------+
| id | `table_name` | rooms |
+------+--------------+-------+
| 1 | foo | rooms |
| 2 | foo | rooms |
+------+--------------+-------+
2 rows in set (0.00 sec)
将为真:
DROP PROCEDURE IF EXISTS pr;
DROP TABLE IF EXISTS t;
CREATE PROCEDURE pr (IN T_NAME VARCHAR(255))
SELECT `id`, `table_name`, T_NAME FROM t
;
CREATE TABLE t (id INT, `table_name` VARCHAR(255));
INSERT INTO t VALUES (1,'hotels'),(2,'rooms');
CALL pr("rooms");
CALL pr("foo");
您需要的是参数的不同名称:
table_name = T_NAME
现在比较MariaDB [test]> CALL pr("rooms");
+------+------------+--------+
| id | table_name | T_NAME |
+------+------------+--------+
| 1 | hotels | rooms |
| 2 | rooms | rooms |
+------+------------+--------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> CALL pr("foo");
+------+------------+--------+
| id | table_name | T_NAME |
+------+------------+--------+
| 1 | hotels | foo |
| 2 | rooms | foo |
+------+------------+--------+
2 rows in set (0.00 sec)
实际上是有道理的;
use unique_type_id::UniqueTypeId;
#[derive(UniqueTypeId)]
struct Test1;
#[derive(UniqueTypeId)]
struct Test2;
assert_eq!(Test1::id().0, 1u64);
assert_eq!(Test2::id().0, 2u64);