Reputation: 3493
I have this trigger that cointains a temporary table. Now I wish to loop through the temporary table and do something on each row. Is there some easy way of doing this? I have googled but everything seems overly complicated for such a simple task.
CREATE TRIGGER myTrigger AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
IF NEW.col <> OLD.col THEN
DROP TEMPORARY TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE IF NOT EXISTS tmpTable AS (my select statement);
--For each row in tempTable--
Do something
--
DROP TEMPORARY TABLE tmpTable;
END IF;
END
Upvotes: 1
Views: 5271
Reputation: 1394
to have rownumber You can do:
DROP TEMPORARY TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE IF NOT EXISTS tmpTable AS (
SELECT l.*,
@curRow := @curRow + 1 AS row_no
FROM XXX_TABLE l
JOIN (SELECT @curRow := 0) r);
to use it You can use WHILE
loop:
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM tmpTable INTO n;
SET i=0;
WHILE i<n DO
SET i = i + 1;
-- do sth WHERE row_no=i;
END WHILE;
Upvotes: 6