编程专栏 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。后面才是一些处理的语句。

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。