mysql probleme,“ else”在此位置无效,应为:END

时间:2019-02-26 11:09:13

标签: mysql stored-procedures mysql-workbench

我正在使用MySQL,但是只要我是初学者,我就需要一些帮助来理解并解决这个问题

delimiter ./
create procedure getMesuresBetweenDates (in debut timestamp , in fin timestamp , in nomWaspmote varchar(16) , in nomSensor varchar(16) , out resultat int)
begin
declare compte int;
set compte = NULL;
If debut>=fin then set resultat = -2; end if;
else If Not Exists sensorParser then set resultat = -1 ; end if;
else select @compte=count(*) value, timestamp from sensorParser where id_wasp = nomWaspmote and sensor = nomSensor and timestamp >= debut and timestamp <= fin;
if @compte=0 then set resultat = 0; end if;
else set resultat = @compte;
end ./

这是第一个不起作用的东西,当我飞越它时,它告诉“ else”在此位置无效,期望是:END

我是法国人,所以我希望我的英语还不错

这里是新代码,多亏了Bart:

delimiter ./
create procedure getMesuresBetweenDates (in debut timestamp , in fin timestamp , in nomWaspmote varchar(16) , in nomSensor varchar(16) , out resultat int)
begin
declare compte int;
set compte = NULL;
If debut>=fin then 
    set resultat = -2;
else If Not Exists (select * from sensorParser) then 
    set resultat = -1 ;
else select @compte=count(*) value, timestamp from sensorParser where id_wasp = nomWaspmote and sensor = nomSensor and timestamp >= debut and timestamp <= fin;
if @compte=0 then
    set resultat = 0;
else set resultat = @compte;
end if;
end ./

它以某种方式工作,但是现在我对end还有另一个问题:它告诉我该语句是不完整的,它期望一个IF,但是我不知道在哪里

2 个答案:

答案 0 :(得分:0)

仅在CREATE TABLE `society_flat` ( `id` int(11) NOT NULL, `block_id` int(11) NOT NULL, `flat_no` int(5) NOT NULL, `owner_name` varchar(200) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `society_flat` (`id`, `block_id`, `flat_no`, `owner_name`) VALUES (1, 1, 101, 'Mahavir'), (2, 1, 102, 'Pankaj'), (3, 2, 101, 'Krunal'), (4, 2, 102, 'Sanjay'), (7, 1, 103, 'Dipak '), (9, 2, 103, 'Dipak p'), (11, 1, 104, 'Pankaj'), (12, 1, 201, 'Pravin bhai'), (13, 1, 204, 'mandip'), (14, 1, 203, 'Banti'), (16, 2, 104, 'Bhavesh bhai'), (17, 1, 202, 'Rakesh'); 之后,您不应该以{{1​​}}结尾所有if块:

end if

作为一般说明,在创建类似这样的复杂结构时,请确保您打算正确,以保持可读性。

如果要在一个else中执行多个If debut>=fin then set resultat = -2; -- don't put "end if" here else -- do something else end if; ,则必须使用else

if

如果按照编写的方式进行操作,则必须用elseif关闭所有If debut>=fin then set resultat = -2; -- don't put "end if" here elseif fin>=debut then -- do something else else -- do yet something else end if;

if

有关更多信息,请参见the documentation

答案 1 :(得分:0)

delimiter ./
create procedure getMesuresBetweenDates (in debut timestamp , in fin timestamp , in nomWaspmote varchar(16) , in nomSensor varchar(16) , out resultat int)
begin
declare compte int;
set compte = NULL;
If debut>=fin then 
    set resultat = -2; 
else If Not Exists sensorParser then 
    set resultat = -1 ; 
else 
     select @compte=count(*) value, timestamp from sensorParser where id_wasp = nomWaspmote and sensor = nomSensor and timestamp >= debut and timestamp <= fin;
end if;
if @compte=0 then 
   set resultat = 0; 
else set resultat = @compte;
end if;
end ./

如果结束;太多,放在错误的地方