gpt4 book ai didi

mysql - 将不同记录从一个表复制到另一个表的游标

转载 作者:行者123 更新时间:2023-11-28 23:41:13 25 4
gpt4 key购买 nike

我想将所有 recoreds 从 temp1 表复制到我为此使用游标的另外两个表。

 DELIMITER //
CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);

declare tcursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
OPEN tcursor;
REPEAT
FETCH cursor into v_fn,v_ln, v_email;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fname,v_lname,v_email);
END REPEAT;
CLOSE tcursor;
END//

DELIMITER

这段代码显示错误

 MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLE' at line 8

如何解决这个问题

最佳答案

您的语法中有多个错误,请不要退出循环。试试这个?

CREATE PROCEDURE cpyQ()
BEGIN
DECLARE g_id INT DEFAULT 0;
DECLARE v_fn varchar(100);
DECLARE v_ln varchar(100);
DECLARE v_email varchar(100);
DECLARE done INT DEFAULT FALSE;

declare tcursor cursor for select distinct mailid,fname,lname from temp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tcursor;
read_loop: LOOP
FETCH tcursor into v_fn,v_ln, v_email;
if done then
LEAVE read_loop;
END IF;
insert into atom(type) values('Person');
SET g_id = LAST_INSERT_ID();
insert into user(id,fname,lname,mailid) values(g_id,v_fn,v_ln,v_email);

END LOOP;
CLOSE tcursor;
END

关于mysql - 将不同记录从一个表复制到另一个表的游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com