如果存在,则更新 else 插入,在存储过程中使用游标 returns 只有 1 行
if exists, update else insert, with cursors in stored procedures returns only 1 row
我有 2 tables api(列 - api , api_context, api_id --> 主键) 和 api_request_summary(列 - api_context)。我需要插入与特定 api_id 相关的 api_context 在 table curhittest1 中重复的次数。也就是说,我需要计算 api_context 列中的值重复了多少次,并将其与 api_id 一起插入。两个 tables 都有多行,所以我使用游标循环遍历 tables。我可以正确地将值插入 table 但我需要检查 api_id 是否已经存在于 curhittest1 table 中,如果是则更新,如果没有则插入。
DELIMITER //
CREATE PROCEDURE curhit12()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE apiId, noOfHits int(11);
Declare apiContext varchar(255);
DECLARE cur1 CURSOR FOR Select api_id from api;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO apiId;
IF done THEN
LEAVE read_loop;
END IF;
select api_context into apiContext from api where api_id =apiId;
SELECT COUNT(*) FROM api_request_summary WHERE api_context=apiContext into noOfHits;
IF exists (SELECT * FROM curhittest1 WHERE apiid = apiId) THEN
UPDATE curhittest1
SET noofhits=noOfHits
WHERE apiid = apiId;
ELSE
insert into curhittest1(apiid,noofhits) values (apiId,noOfHits);
END IF;
END LOOP;
CLOSE cur1;
END//
DELIMITER
当我使用下面的代码时,只有 1 行被添加到 curhittest1 table。当在没有游标的 sql 过程中使用完全相同的代码时,它会显示,所以我假设在使用游标时我需要做一些不同的事情。我怎样才能准确地将所有值添加到 table?
循环中的第二个 INTO
语句可能会触发 NOT FOUND
处理程序。
更改此行:
SELECT COUNT(*) FROM api_request_summary WHERE api_context=apiContext into noOfHits;
为此:
SET noOfHits = (SELECT COUNT(*) FROM api_request_summary
WHERE api_context=apiContext);
它在使用 INSERT ... ON DUPLICATE KEY UPDATE 后有效。对于任何感兴趣的人,这里是代码
DELIMITER //
CREATE PROCEDURE curhit12()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE apiId, noOfHits int(11);
Declare apiContext varchar(255);
DECLARE cur1 CURSOR FOR Select api_id from api;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO apiId;
IF done THEN
LEAVE read_loop;
END IF;
select api_context into apiContext from api where api_id =apiId;
SET noOfHits = (SELECT COUNT(*) FROM api_request_summary
WHERE api_context=apiContext);
INSERT INTO api_hits (api_id,no_of_hits) VALUES (apiId,noOfHits)
ON DUPLICATE KEY UPDATE no_of_hits=noOfHits;
END LOOP;
CLOSE cur1;
END//
DELIMITER
我有 2 tables api(列 - api , api_context, api_id --> 主键) 和 api_request_summary(列 - api_context)。我需要插入与特定 api_id 相关的 api_context 在 table curhittest1 中重复的次数。也就是说,我需要计算 api_context 列中的值重复了多少次,并将其与 api_id 一起插入。两个 tables 都有多行,所以我使用游标循环遍历 tables。我可以正确地将值插入 table 但我需要检查 api_id 是否已经存在于 curhittest1 table 中,如果是则更新,如果没有则插入。
DELIMITER //
CREATE PROCEDURE curhit12()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE apiId, noOfHits int(11);
Declare apiContext varchar(255);
DECLARE cur1 CURSOR FOR Select api_id from api;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO apiId;
IF done THEN
LEAVE read_loop;
END IF;
select api_context into apiContext from api where api_id =apiId;
SELECT COUNT(*) FROM api_request_summary WHERE api_context=apiContext into noOfHits;
IF exists (SELECT * FROM curhittest1 WHERE apiid = apiId) THEN
UPDATE curhittest1
SET noofhits=noOfHits
WHERE apiid = apiId;
ELSE
insert into curhittest1(apiid,noofhits) values (apiId,noOfHits);
END IF;
END LOOP;
CLOSE cur1;
END//
DELIMITER
当我使用下面的代码时,只有 1 行被添加到 curhittest1 table。当在没有游标的 sql 过程中使用完全相同的代码时,它会显示,所以我假设在使用游标时我需要做一些不同的事情。我怎样才能准确地将所有值添加到 table?
循环中的第二个 INTO
语句可能会触发 NOT FOUND
处理程序。
更改此行:
SELECT COUNT(*) FROM api_request_summary WHERE api_context=apiContext into noOfHits;
为此:
SET noOfHits = (SELECT COUNT(*) FROM api_request_summary
WHERE api_context=apiContext);
它在使用 INSERT ... ON DUPLICATE KEY UPDATE 后有效。对于任何感兴趣的人,这里是代码
DELIMITER //
CREATE PROCEDURE curhit12()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE apiId, noOfHits int(11);
Declare apiContext varchar(255);
DECLARE cur1 CURSOR FOR Select api_id from api;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO apiId;
IF done THEN
LEAVE read_loop;
END IF;
select api_context into apiContext from api where api_id =apiId;
SET noOfHits = (SELECT COUNT(*) FROM api_request_summary
WHERE api_context=apiContext);
INSERT INTO api_hits (api_id,no_of_hits) VALUES (apiId,noOfHits)
ON DUPLICATE KEY UPDATE no_of_hits=noOfHits;
END LOOP;
CLOSE cur1;
END//
DELIMITER