【问题标题】:mysql probleme, "else" is not valid at this position, expecting : ENDmysql问题,“else”在这个位置无效,期待:END
【发布时间】:2019-07-19 22:00:54
【问题描述】:

我正在使用 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,当我飞过它时,它告诉“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 但我不知道在哪里

【问题讨论】:

    标签: mysql stored-procedures mysql-workbench


    【解决方案1】:

    您不应仅在 else 之后以 end if 结束所有 if 块:

    If debut>=fin then 
         set resultat = -2; 
         -- don't put "end if" here
    else 
         -- do something else
    end if;
    

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

    如果你想在一个if中做更多的elses,你必须使用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;
    

    如果你按照你写的方式做,你必须用end if关闭所有ifs:

    if debut >= fin then
        -- do something
    else if fin >= debut then        -- you start a new "if" here!
                                     -- notice the space between else and if
        -- do the else
         end if                      -- end the second if
    end if                           -- end the first if
    

    更多信息请参见the documentation

    【讨论】:

      【解决方案2】:
      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 ./
      

      结束如果;太多并且设置在错误的地方

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-05
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        • 2020-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多