Phoen
Phoen

Reputation: 277

mysql if without select

I'm using mysql and I want to use the if statement in a way you are doing in a stored procedure. Something like this:

delimiter $$

  if @myVariable is null then
    drop temporary table tmp_buffer;
    select 'cannot proceed without variable @myVariable';
  else
    update my_table as t
    set t.name = @myVariable;
  end;

end$$

delimiter ;

When I execute this code it does nothing. I tried to google this but I only find select if(....) explanations what doesn’t fit to my requirements. I know that it is possible to create a stored procedure and put the code there and then call the procedure, but I'm looking for a way to do it without stored procedures. Is this possible? If yes, what is wrong in my code?

Thanks for reading this

Felix

Upvotes: 1

Views: 1737

Answers (1)

Rahul
Rahul

Reputation: 77876

but I'm looking for a way to do it without stored procedures. Is this possible?

NO; as already commented above, you cann't use if .. else construct block like the way you are intend to use in a normal SQL query. You will have to wrap it inside a procedural block which could be a stored procedure or a function.

Upvotes: 4

Related Questions