DROP PROCEDURE IF EXISTS ProA()
begin
#创建临时表
DROP TEMPORARY TABLE IF EXISTS table_a
CREATE TEMPORARY TABLE table_temp_b(
`id` int(19) NOT NULL,
`name` varchar(50) DEFAULT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
#联合查询c,d两表的数据插入数据到临时表b
insert into table_temp_b(id, name) select id,name from c,d where c.id = d.id;
#使用游标查询临时表
DECLARE pCursor CURSOR FOR SELECT id, `name` FROM table_temp_b;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN pCursor;
pCurlorLoop : LOOP
FETCH pCursor INTO id, type;
SELECT id;
IF done = 1 THEN
LEAVE pCurlorLoop;
END IF;
END LOOP pCurlorLoop;
CLOSE pCursor;
end
总是提示DECLARE游标报错,即使是声明普通变量也会报错,怎么搞,困了本人很久了?
begin
#创建临时表
DROP TEMPORARY TABLE IF EXISTS table_a
CREATE TEMPORARY TABLE table_temp_b(
`id` int(19) NOT NULL,
`name` varchar(50) DEFAULT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
#联合查询c,d两表的数据插入数据到临时表b
insert into table_temp_b(id, name) select id,name from c,d where c.id = d.id;
#使用游标查询临时表
DECLARE pCursor CURSOR FOR SELECT id, `name` FROM table_temp_b;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN pCursor;
pCurlorLoop : LOOP
FETCH pCursor INTO id, type;
SELECT id;
IF done = 1 THEN
LEAVE pCurlorLoop;
END IF;
END LOOP pCurlorLoop;
CLOSE pCursor;
end
总是提示DECLARE游标报错,即使是声明普通变量也会报错,怎么搞,困了本人很久了?
解决方案:20分
done、id、type在使用之前都没有定义变量。
解决方案:20分
DROP PROCEDURE IF EXISTS ProA()
begin
declare done int;
declare id int;
declare type varchar(50);
#创建临时表
DROP TEMPORARY TABLE IF EXISTS table_a
CREATE TEMPORARY TABLE table_temp_b(
`id` int(19) NOT NULL,
`name` varchar(50) DEFAULT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
#联合查询c,d两表的数据插入数据到临时表b
insert into table_temp_b(id, name) select id,name from c,d where c.id = d.id;
#使用游标查询临时表
DECLARE pCursor CURSOR FOR SELECT id, `name` FROM table_temp_b;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN pCursor;
pCurlorLoop : LOOP
FETCH pCursor INTO id, type;
SELECT id;
IF done = 1 THEN
LEAVE pCurlorLoop;
END IF;
END LOOP pCurlorLoop;
CLOSE pCursor;
end
begin
declare done int;
declare id int;
declare type varchar(50);
#创建临时表
DROP TEMPORARY TABLE IF EXISTS table_a
CREATE TEMPORARY TABLE table_temp_b(
`id` int(19) NOT NULL,
`name` varchar(50) DEFAULT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
#联合查询c,d两表的数据插入数据到临时表b
insert into table_temp_b(id, name) select id,name from c,d where c.id = d.id;
#使用游标查询临时表
DECLARE pCursor CURSOR FOR SELECT id, `name` FROM table_temp_b;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN pCursor;
pCurlorLoop : LOOP
FETCH pCursor INTO id, type;
SELECT id;
IF done = 1 THEN
LEAVE pCurlorLoop;
END IF;
END LOOP pCurlorLoop;
CLOSE pCursor;
end