Bug #61895 Enable "IF" statements to be used in scripts outside of stored procedures
Submitted: 18 Jul 2011 13:49 Modified: 19 Jul 2011 11:46
Reporter: Paul Poulain Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: General Severity:S4 (Feature request)
Version:5.5 OS:Any
Assigned to: CPU Architecture:Any

[18 Jul 2011 13:49] Paul Poulain
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).
[18 Jul 2011 14:05] Valeriy Kravchuk
I think this is just a subset of know feature request, bug #48777. Please, check.
[19 Jul 2011 8:29] Paul Poulain
Hi Valeriy,

Agreed that this is a duplicate, sorry! I did search for "if statement" ("if" on it's own was excluded from the search) and scanned through over 150 matches, but I clearly missed it.

Many thanks,

Paul.
[19 Jul 2011 11:46] Valeriy Kravchuk
Duplicate of bug #48777.