Vishal
Vishal

Reputation: 2336

MySQL IF version < x THEN ELSE

How would I write an IF ... THEN ... ELSE within MySQL where my IF is testing for the server version number? Pointers to the correct section of the docs are appreciated. More details are really appreciated.

Update:

Apologies for an unintentionally vague question.

I keep a sql script ready to create databases and users easily when I deploy something on machines (new to me). Different servers are running different versions of the software and I wanted to make the script reliable and version-aware.

Upvotes: 2

Views: 947

Answers (3)

Bill Karwin
Bill Karwin

Reputation: 562368

It's hard to answer this because you haven't described what you're trying to do, but you may like to know about a neat feature of MySQL SQL comment syntax to make some parts of a query version-dependent.

See http://dev.mysql.com/doc/refman/5.7/en/comments.html

If you add a version number after the “!” character, the syntax within the comment is executed only if the MySQL version is greater than or equal to the specified version number. The TEMPORARY keyword in the following comment is executed only by servers from MySQL 3.23.02 or higher:

CREATE /*!32302 TEMPORARY */ TABLE t (a INT);

Upvotes: 2

F.Igor
F.Igor

Reputation: 4350

  • Use the VERSION() function or @@VERSION variable to get the current mysql version.
  • Use the IF( condition , value_if_true, value_if_false) function to make a basic testing.
  • Use a combination of string functions like SUBSTRING, VERSION, and LOCATE to extract some part of the version string.

Ej: (mayor version number, from '5.6.26-74.0-log' extracts '5')

SELECT SUBSTRING( @@VERSION , 1 , LOCATE( '.' , @@VERSION ) )

Upvotes: 0

Charles
Charles

Reputation: 4352

You'll need to use the version global variable.

mysql> select @@VERSION;
+-----------------+
| @@VERSION       |
+-----------------+
| 5.6.26-74.0-log |
+-----------------+
1 row in set (0.00 sec)

Upvotes: 0

Related Questions