Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using MySQL but as long as I'm a beginner I need some help do understand and solve this problem

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 ./

It's the first else which doesn't work, when I overfly it, it tells "else' is not valid at this position, expecting : END

I'm French so I hope my english isn't too bad

Here is the new code, thanks to 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 ./

it worked in a way but now I have another problem with end : it tells me that the statement is incomplete, it's expecting an IF but I don't know where

as a general note, when creating complex structures like this, make sure you intend correctly, to keep it readable.

If you want to do more elses in one if, you have to use elseif:

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

If you do it in the way you have written, you have to close all ifs with an end if:

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

See the documentation for more information.

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 ; 
     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 ./

end if; too many and set in wrong places

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.