我对这个存储过程感到很疯狂。 我发布完整的转储希望有人可以解释我的错误。
drop table if exists questions;
create table questions (
id int not null auto_increment primary key,
description varchar(200)
) engine = myisam;
insert into questions (description)
values
('Question 1'),
('Question 2'),
('Question 3');
drop table if exists answers;
create table answers (
id int not null auto_increment primary key,
question_id int,
description varchar(10)
) engine = myisam;
insert into answers (question_id,description)
values
(1,'no'),
(1,'no'),
(1,'yes'),
(1,'no'),
(1,'yes'),
(2,'no'),
(2,'yes'),
(2,'yes'),
(2,'yes'),
(2,'no'),
(2,'no'),
(2,'no'),
(2,'yes'),
(2,'no'),
(3,'no'),
(3,'no'),
(3,'no'),
(3,'yes');
这是我的存储过程:
delimiter //
drop procedure if exists dynamic_view //
create procedure dynamic_view()
begin
declare cnt int default 0;
declare finito int default 0;
declare qid int;
declare qdescription varchar(100);
declare qanswer varchar(100);
declare i int default 1;
declare j int;
declare str varchar(20000) default '';
declare str2 varchar(20000);
declare str3 varchar(20000);
declare curs1 cursor for select id,description from questions order by id;
declare curs2 cursor for select description from answers where question_id = qid;
declare continue handler for not found set finito = 1;
-- drop table if exists tmp;
select count(*) as num into @cnt from answers group by question_id order by num desc limit 1;
drop table if exists tmp;
while i <= @cnt do
set str = concat(str,'answer',i,' varchar(10),');
set i = i + 1;
end while;
set str = substr(str,1,char_length(str)-1);
set @str = concat('create temporary table tmp (id int,question varchar(200),',str,');');
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
open curs1;
mio_loop:loop
fetch curs1 into qid,qdescription;
if finito = 1 then
close curs1;
leave mio_loop;
end if;
open curs2;
set str2 = 'insert into tmp (id,question,';
set j = 1;
set str3 = '';
mio_loop2:loop
fetch curs2 into qanswer;
if finito = 1 then set finito = 0;
close curs2;
leave mio_loop2;
end if;
set str2 = concat(str2,'answer',j,',');
set str3 = concat(str3,"'",qanswer,"',");
set j = j + 1;
end loop mio_loop2;
set str2 = substr(str2,1,char_length(str2)-1);
set str3 = substr(str3,1,char_length(str3)-1);
set @str2 = concat(str2,') values (',qid,",'",qdescription,"',",str3,');');
prepare stmt from @str2;
execute stmt;
deallocate prepare stmt;
end loop mio_loop;
select * from tmp;
end; //
delimiter ;
当我打电话给我时,我得到了这个输出
mysql> call dynamic_view();
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| id | question | answer1 | answer2 | answer3 | answer4 | answer5 | answer6 | answer7 | answer8 | answer9 |
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| 1 | Question 1 | no | no | yes | no | yes | NULL | NULL | NULL | NULL |
| 2 | Question 2 | no | yes | yes | yes | no | no | no | yes | no |
| 3 | Question 3 | no | no | no | yes | NULL | NULL | NULL | NULL | NULL |
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
3 rows in set (0.23 sec)
现在让我们假设我删除了一个有question_id = 2的记录。 这是具有最大记录数的id,即9.如果我删除它,则记录变为8,所以最后一列必须消失。
delete from answers where id = 14;
如果我记得我的sp,我会收到此错误:
Error Code : 1054
Unknown column 'pivot.tmp.answer9' in 'field list'
但是,如果我再次执行存储过程的整个代码,然后我记得它得到了正确的结果:
mysql> call dynamic_view();
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+
| id | question | answer1 | answer2 | answer3 | answer4 | answer5 | answer6 | answer7 | answer8 |
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+
| 1 | Question 1 | no | no | yes | no | yes | NULL | NULL | NULL |
| 2 | Question 2 | no | yes | yes | yes | no | no | no | yes |
| 3 | Question 3 | no | no | no | yes | NULL | NULL | NULL | NULL |
+------+------------+---------+---------+---------+---------+---------+---------+---------+---------+
3 rows in set (0.19 sec)
我无法理解为什么我放弃了临时表。提前致谢。 编辑。我想开始赏金,但我找不到按钮。 :)
我刚读过meta,我可以在两天后开始赏金。从未注意到它。希望有人可以帮助我:))
答案 0 :(得分:1)
有一次我看到类似的问题。请看一下这个链接 - unable to add columns dynamically to table using alter
尼克,我建议您使用预准备语句从临时表输出数据(...这个表是在SP中使用预准备语句创建的,这看起来像一个错误 - SELECT * inside PROCEDURE gives "Unknown column" on second loop if tbl changed)。< / p>
只需更改最后一行 -
select * from tmp';
使用此代码 -
PREPARE stmt FROM 'select * from tmp';
EXECUTE stmt;
DEALLOCATE PREPARE stmt;