[英]if exists, update else insert, with cursors in stored procedures returns only 1 row
I have 2 tables api(columns - api , api_context, api_id -->primary key) and api_request_summary(columns - api_context). 我有2个表api(columns-api,api_context,api_id->主键)和api_request_summary(columns-api_context)。 i need to insert the number of times the api_context relevant to a particular api_id is repeated in the table curhittest1.
我需要在表curhittest1中插入与特定api_id相关的api_context重复的次数。 That is I need to get the count of how many times a value in the column api_context is repeated and insert it with the api_id.
那就是我需要获得对api_context列中的值重复多少次的计数,并将其与api_id一起插入。 Both tables have multiple rows so i have used cursors to loop through the tables.
两个表都有多行,所以我使用游标在表中循环。 i can correctly insert the values to the table but i need to check whether that api_id already exists in the curhittest1 table and if yes update and if no insert.
我可以正确地将值插入到表中,但是我需要检查curhittest1表中是否已经存在api_id,如果是,请更新,如果没有,请插入。
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
When I use the following code only 1 row is added to the curhittest1 table. 当我使用以下代码时,仅将1行添加到curhittest1表中。 When the exact same code is used in sql procedures without cursors it displays so i'm assuming i need to do something differently when using with cursors.
当在没有游标的sql过程中使用完全相同的代码时,它将显示出来,因此我假设与游标一起使用时,我需要做一些不同的事情。 How can i add all the values to the table accurately?
如何将所有值准确地添加到表中?
The second INTO
sentence inside loop maybe trigger the NOT FOUND
handler. 循环内的第二个
INTO
语句可能触发NOT FOUND
处理程序。
Change this line: 更改此行:
SELECT COUNT(*) FROM api_request_summary WHERE api_context=apiContext into noOfHits;
To this: 对此:
SET noOfHits = (SELECT COUNT(*) FROM api_request_summary
WHERE api_context=apiContext);
It worked after using INSERT ... ON DUPLICATE KEY UPDATE. 使用INSERT ... ON DUPLICATE KEY UPDATE后,它可以工作。 For anyone interested here is the code
对于任何对此感兴趣的人都是代码
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.