Description:
Hi,
I was frustrated to find out that the IF statement can't be used outside of stored procedures/functions. A cursory search of the bug databased found a report for this (http://bugs.mysql.com/bug.php?id=15037) unhelpfully classified as "not a bug" over 6 years ago.
Firstly, I appreciate it might be "as designed", but the documentation isn't very clear (at least the 5.6 documentation I've just checked). Unless I'm going blind, it doesn't actually say that an "IF" can't be used outside of a stored procedure or function. All I can see is
"An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon, as shown in this example: "...
I appreciate that the parent topics "12.7 MySQL Compound-Statement Syntax" and "12.7.6 Flow Control Constructs" both mention "within stored procedures" but again, it doesn't explicitly state that they don't work outside.
However, rather than fixing the documentation, I'd prefer to see this implemented. It seems like pretty basic functionality that was raised 6 years ago and still hasn't been addressed. Whilst the proposed workaround (creating a stored proc) will probably work in most situations, it is clearly a XXX bodge and requires that the caller has create (or at least alter) procedure\function permissions which isn't always desirable.
I'm just in the middle of porting a suite of applications from SQL Server to MySQL, and for the most part, MySQL is great, but it is frustrating when you run into missing simple functionality like this.
Many thanks,
Paul.
How to repeat:
(Ripped from the previous bug report)
mysql> if (1>0) then select "hello"
-> end if;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'if (1>0) then
select "hello"
end if' at line 1
Suggested fix:
Implement the use of flow control statements outside of stored procedures an functions (or least update the documentation to make this clearer).