+ -
当前位置:首页 → 问答吧 → 临时表、存储过程线性执行问题?

临时表、存储过程线性执行问题?

时间:2011-07-21

来源:互联网

问题概述:在存储过程中将查询的数据insert 到一张临时表中,在最终语句中对临时表进行select
简要代码:
方案一
create procedure test
begin
  declare tmpsql,sqlstr text;
  while 条件 do
  
  insert into 临时表名 select a.* from 表名 a where id>oldid and id<Y '; 
   
  end while;
  select * from 临时表名;
end;

这种做法可能表述不是很明确,我觉得奇怪,语句执行是由上往下的执行过程,但是这与如下方法有什么区别。
create procedure test
begin
  declare tmpsql,sqlstr text;
  while 条件 do 
  set tmpsql='select a.* from 表名 a where id>oldid and id<Y ';  
  end while;
  set tmpsql=concat('select * from (',tmpsql,') a');
  prepare sqlstr from tmpsql;
  execute sqlstr;
end;

作者: ls3648098   发布时间: 2011-07-21

1 你下面的似乎漏掉了union all
2 可能考虑N个单个insert select可能会比N个union all更快,这个要实测
3 临时表是否存在约束,导致insert的时候会出问题,而这正是他的目的?

作者: shine333   发布时间: 2011-07-21