为什么在mysql存储过程中创建的临时表不能自动销毁??

woshigefantong 2008-06-19 10:47:08
BEGIN
declare idlower bigint;
declare idupper bigint;
declare totalreccount int;
declare mymod bigint;
CREATE TEMPORARY TABLE tmp_table15 (
rowid bigint auto_increment primary key,
userid bigint);
insert into tmp_table15 (userid) select ID from restcomments;
set idlower=(pageno-1)*pagesize+1;
set idupper=pageno*pagesize;
select * from tmp_table15;

/*计算总页数*/
select count(*) from tmp_table12 into totalreccount;
set pagecount=totalreccount;


drop table tmp_table2;

按说TEMPORARY的表在每次存储过程执行结束以后就自动销毁了,可是我这个存储过程每次执行过后再执行都说“tmp_table15 已存在”,我设置了tmp_table15 是临时的了啊,怎么执行完以后还会存在呢?
在数据库的表中还看不到这个表,连用drop tmp_table15都不行,到底是为什么啊





END
...全文
1085 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshigefantong 2008-06-20
  • 打赏
  • 举报
回复
看了你的代码,问题终于顺利解决了
谢谢了
好崇拜啊
懒得去死 2008-06-20
  • 打赏
  • 举报
回复
仔细检查!!!!!我给你修改的。



delimiter ||
create procedure sp_test1(
IN pageno int, IN pagesize int,
OUT pagecount int
)
BEGIN
declare idlower bigint;
declare idupper bigint;
declare totalreccount int;

drop table if exists tmp_table21;
CREATE TEMPORARY TABLE tmp_table21(
rowid bigint auto_increment primary key,
userid bigint
);


insert into tmp_table21 (userid) select ID from restcomments;
set idlower=(pageno-1)*pagesize+1;
set idupper=pageno*pagesize;
select * from tmp_table21;

/*计算总页数*/
select count(*) from tmp_table21 into totalreccount;
set pagecount=totalreccount;
end||
delimiter ;


wwwwb 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 woshigefantong 的回复:]
我试图使用你的方法,在存储过程的前面加上如果表已经存在就删除这个表
可是运行的时候,总是报“未知的table tmp_table21 ”
这是为什么啊?教我一下好吗
if exists TEMPORARY table tmp_table21 then
drop TEMPORARY table tmp_table21;
end if;
[/Quote]
DROP TEMPORARY TABLE
IF EXISTS tmp_table21;
woshigefantong 2008-06-20
  • 打赏
  • 举报
回复
我试图使用你的方法,在存储过程的前面加上如果表已经存在就删除这个表
可是运行的时候,总是报“未知的table tmp_table21 ”
这是为什么啊?教我一下好吗
if exists TEMPORARY table tmp_table21 then
drop TEMPORARY table tmp_table21;
end if;
懒得去死 2008-06-19
  • 打赏
  • 举报
回复
drop table tmp_table2;
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
如果是上述代码的话,你DROP的是
drop table tmp_table2

tmp_table15
woshigefantong 2008-06-19
  • 打赏
  • 举报
回复
我在存储过程的最后也写了drop table tmp_table15了,可是再次运行还是老样子
如果说是 关闭连接时DROP
那不是说一个创建临时表的存储过程在一次连接中只能使用一次吗?这个好像不大合理啊
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
按说TEMPORARY的表在每次存储过程执行结束以后就自动销毁了
NO,MYSQL停止运行
OR
A TEMPORARY table is visible only to the current connection, and is dropped automatically
when the connection is closed

关闭连接时DROP

用DROP TABLE 应该可以删除
woshigefantong 2008-06-19
  • 打赏
  • 举报
回复
类似的东西我在sql server上都做过
一切正常,怎么到了mysql总出毛病呢
懒得去死 2008-06-19
  • 打赏
  • 举报
回复
Move this statement drop table tmp_table2 to the beginning of your procedure.


Use statement drop table if exists tmp_table2 is better.
WWWWA 2008-06-19
  • 打赏
  • 举报
回复
用临时表比较 “干净”
apache6 2008-06-19
  • 打赏
  • 举报
回复
是否在其他地方有用到tmp_table15表的地方,把表锁住了。
我就奇怪了,你每次都要生成相同的表结构,为什么不只是每次清空表,然后插入数据?非要每次删除表!
WWWWA 2008-06-19
  • 打赏
  • 举报
回复
重新贴一下你的储存过程
woshigefantong 2008-06-19
  • 打赏
  • 举报
回复
我的权限就是数据库管理员啊
权限方面没有任何问题
WWWWA 2008-06-19
  • 打赏
  • 举报
回复
DROP时有没有提示?
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
检查一下对临时表操作的权限
懒得去死 2008-06-19
  • 打赏
  • 举报
回复
Do you have privilege to drop your temporary table?
woshigefantong 2008-06-19
  • 打赏
  • 举报
回复
drop table tmp_table15这个我运行的时候已经改过来了
但是还是不行
到底是为什么啊

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧