MySQL - 将输入参数传递给 Cursor 查询
MySQL - Pass input parameter into Cursor query
是否可以将输入参数传递到 Cursor SELECT 语句 WHERE 子句中?
出于某种原因,我认为它不起作用。
我正在尝试将 _TAG
和 _ITEM_NAME
传递到 where 子句中。
DECLARE cursor_test cursor for SELECT itemid FROM items WHERE key_ LIKE "sometext_@_TAG_sometext_@_ITEM_NAME" AND STATUS = '0';
这是存储过程:
DELIMITER //
CREATE PROCEDURE getSomething(IN _HOSTNAME VARCHAR(20), _TAG VARCHAR(20), _ITEM_NAME VARCHAR(50))
BEGIN
declare FINISHED BOOL default false;
DECLARE cursor_test cursor for SELECT itemid FROM items WHERE hostid = @_HOSTID AND key_ LIKE "sometext_@_TAG_sometext_@_ITEM_NAME" AND STATUS = '0';
DECLARE CONTINUE HANDLER for not found set FINISHED := true;
SET @HOSTNAME = _HOSTNAME;
PREPARE STMT1 FROM
"SELECT hostid INTO @_HOSTID FROM hosts WHERE NAME = ?";
EXECUTE STMT1 USING @HOSTNAME;
DEALLOCATE PREPARE STMT1;
open cursor_test;
SET @TOTAL_VALUE := 0;
loop_itemid: loop
fetch cursor_test into _ITEMID;
SELECT _ITEMID;
if FINISHED then
leave loop_itemid;
end if;
SET @TOTAL_VALUE := @TOTAL_VALUE + (SELECT value from history_uint WHERE itemid = _ITEMID ORDER BY clock DESC LIMIT 1);
end loop loop_itemid;
SELECT @TOTAL_VALUE;
close cursor_test;
END //
感谢akina's comment。在条件有效的地方使用 CONCAT。
WHERE key_ LIKE CONCAT('sometext_', _TAG, '_sometext_', _ITEM_NAME)
是否可以将输入参数传递到 Cursor SELECT 语句 WHERE 子句中?
出于某种原因,我认为它不起作用。
我正在尝试将 _TAG
和 _ITEM_NAME
传递到 where 子句中。
DECLARE cursor_test cursor for SELECT itemid FROM items WHERE key_ LIKE "sometext_@_TAG_sometext_@_ITEM_NAME" AND STATUS = '0';
这是存储过程:
DELIMITER //
CREATE PROCEDURE getSomething(IN _HOSTNAME VARCHAR(20), _TAG VARCHAR(20), _ITEM_NAME VARCHAR(50))
BEGIN
declare FINISHED BOOL default false;
DECLARE cursor_test cursor for SELECT itemid FROM items WHERE hostid = @_HOSTID AND key_ LIKE "sometext_@_TAG_sometext_@_ITEM_NAME" AND STATUS = '0';
DECLARE CONTINUE HANDLER for not found set FINISHED := true;
SET @HOSTNAME = _HOSTNAME;
PREPARE STMT1 FROM
"SELECT hostid INTO @_HOSTID FROM hosts WHERE NAME = ?";
EXECUTE STMT1 USING @HOSTNAME;
DEALLOCATE PREPARE STMT1;
open cursor_test;
SET @TOTAL_VALUE := 0;
loop_itemid: loop
fetch cursor_test into _ITEMID;
SELECT _ITEMID;
if FINISHED then
leave loop_itemid;
end if;
SET @TOTAL_VALUE := @TOTAL_VALUE + (SELECT value from history_uint WHERE itemid = _ITEMID ORDER BY clock DESC LIMIT 1);
end loop loop_itemid;
SELECT @TOTAL_VALUE;
close cursor_test;
END //
感谢akina's comment。在条件有效的地方使用 CONCAT。
WHERE key_ LIKE CONCAT('sometext_', _TAG, '_sometext_', _ITEM_NAME)