我有三张桌子 项目(Id),属性(Id),project_attribute(Id,project_id,attribute_id)。
我想在 project_attribute 表格中创建记录,方法是将属性表格中的所有属性用于项目表格中的每个项目。
要创建此类记录,我正在使用以下存储过程。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proj_attr`()
BEGIN
DECLARE proj_done, attribute_done BOOLEAN DEFAULT FALSE;
declare attributeId int(11) default 0;
declare projectId int(11) default 0;
DECLARE curProjects CURSOR FOR SELECT id FROM project order by id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET proj_done = TRUE;
OPEN curProjects;
cur_project_loop: LOOP
FETCH FROM curProjects INTO projectId;
IF proj_done THEN
CLOSE curProjects;
LEAVE cur_project_loop;
END IF;
BLOCK2: BEGIN
DECLARE curAttribute CURSOR FOR SELECT id FROM attribute order by id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET attribute_done = TRUE;
OPEN curAttribute;
cur_attribute_loop: LOOP
FETCH FROM curAttribute INTO attributeId;
IF attribute_done THEN
CLOSE curAttribute;
LEAVE cur_attribute_loop;
END IF;
insert into project_attribute_value(project_id, attribute_id)
values(projectId, attributeId);
END LOOP cur_attribute_loop;
END BLOCK2;
END LOOP cur_project_loop;
END$$
DELIMITER ;
但是,此过程仅为project_attribute表中的1个项目创建记录,即使Project表中有50个项目。预期记录计数是count(projectId)* count(attributeId)。
答案 0 :(得分:32)
试试这个,这肯定会解决你的问题。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proj_attr`()
BEGIN
DECLARE proj_done, attribute_done BOOLEAN DEFAULT FALSE;
declare attributeId int(11) default 0;
declare projectId int(11) default 0;
DECLARE curProjects CURSOR FOR SELECT id FROM project order by id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET proj_done = TRUE;
OPEN curProjects;
cur_project_loop: LOOP
FETCH FROM curProjects INTO projectId;
IF proj_done THEN
CLOSE curProjects;
LEAVE cur_project_loop;
END IF;
BLOCK2: BEGIN
DECLARE curAttribute CURSOR FOR SELECT id FROM attribute order by id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET attribute_done = TRUE;
OPEN curAttribute;
cur_attribute_loop: LOOP
FETCH FROM curAttribute INTO attributeId;
IF proj_done THEN
set proj_done = false;
CLOSE curAttribute;
LEAVE cur_attribute_loop;
END IF;
insert into project_attribute_value(project_id, attribute_id)
values(projectId, attributeId);
END LOOP cur_attribute_loop;
END BLOCK2;
END LOOP cur_project_loop;
END$$
DELIMITER ;
答案 1 :(得分:7)
非常坦率地说,嵌套游标(通常)是个糟糕的主意。您可以使用普通CROSS JOIN
直接获得所需内容,而无需使用光标。
INSERT INTO proj_attr (project, attribute)
SELECT p.id AS projectid, a.id AS attributeid
FROM project p CROSS JOIN attribute a;
答案 2 :(得分:4)
在内部游标中的第一次迭代之后,“attribute_done”变量被设置为“true”。并且它对于每次下一次迭代都是“真实的”。
这会导致每次下一次迭代都跳过内循环。
示例嵌套游标如下所示。
CREATE TABLE `parent` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB
CREATE TABLE `child` (
`a` int(11) DEFAULT NULL,
`b` varchar(20) DEFAULT NULL
) ENGINE=InnoDB
insert into parent values (1),(2),(3);
insert into child values (1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b');
----------------------------------
drop procedure if exists nestedCursor;
create procedure nestedCursor()
BEGIN
DECLARE done1, done2 BOOLEAN DEFAULT FALSE;
DECLARE parentId,childId int;
DECLARE childValue varchar(30);
DECLARE cur1 CURSOR FOR SELECT a FROM parent;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;
open cur1;
loop1: LOOP
FETCH FROM cur1 INTO parentId;
IF done1 THEN
CLOSE cur1;
LEAVE loop1;
END IF;
BLOCK1 : BEGIN
DECLARE cur2 CURSOR FOR SELECT a,b FROM child where a = parentId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;
open cur2;
loop2 : LOOP
FETCH FROM cur2 INTO childId,childValue;
if done2 THEN
CLOSE cur2;
SET done2 = FALSE;
LEAVE loop2;
end if;
select parentId,childId,childValue;
END LOOP loop2;
END BLOCK1;
END loop loop1;
END;