I have the following query that I am trying to execute. It is the starting point for a query required to run a report:
SET @fname = 'Bob';
SET @blah = 0;
SELECT CASE WHEN @fname IS NULL THEN @blah = 1 ELSE @blah END;
IF (@blah > 0) THEN
SELECT * FROM mytable
END IF;
Something is apparently wrong with my syntax but I cannot figure it out. I’ve written other queries using IF statements without issue. This will not run in either SQLyog or Workbench. It says there is a sytax error on “IF” with or without the parens, doesn’t matter.
All I need is the ability to evaluate a variable/input parameter in the IF statement.
Any help is appreciated. NOTE – the SELECT CASE may not be necessary if I can get the IF to evaluate parameters properly but I was trying any possible solutions.
Thanks
You can use IF-THEN construction only inside BEGIN…END clause, which can be used only in procedures, functions, triggers, and events. It is impossible to use it in a simple script.
For your case I’d suggest this one –