编程专栏 2012-01-31 18:04 采纳率: 0%
浏览 188
已采纳

请教一个MySQL5.0.45存储过程的问题

测试数据表:
drop table if exists pt;
create table pt (
pid int unsigned auto_increment not null,
pname varchar(30) not null,
primary key(pid)
)Engine=InnoDB default charset = utf8;

存储过程代码:
delimiter //
drop procedure if exists p8 //
create procedure p8 (
in param int
)
begin
#if param > 0 then #如果不注释掉这个if ... end if条件会出错,不明白是什么原因?
declare i int default 0;
LOOP_LABEL:loop
insert into pt values(null,'loop');
set i = i+1;
if i >= param then
leave LOOP_LABEL;
end if;
end loop;
#end if;
select * from pt;
end;
//
delimiter ;

存储过程想实现的功能:通过传递进来的参数param往数据表中连续插入一系列记录,但是如果不注释掉存储过程中最外层的if条件,就会出错,不明白是什么原因,刚学存储过程,求指教!

 

补充:但是,为什么改为如下的方式却又是可以正确执行了呢?

delimiter //

drop procedure if exists p8 //

create procedure p8 (

in param int

)

begin

declare i int default 0; #将这条语句的定义提到if条件之前就可以执行了,不明白是什么原因?

if param > 0 then

LOOP_LABEL:loop

insert into pt values(null,'loop');

set i = i+1;

if i >= param then

leave LOOP_LABEL;

end if;

end loop;

end if;

select * from pt;

end;

//

delimiter ;

 

  • 写回答

1条回答 默认 最新

  • yezongnihao 2012-02-01 22:49
    关注

    这是存储过程的基本语法规则。。。begin下面就是declare。后面才是一些处理的语句。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?