0

If I set a session variable (eg. @tmp = 0), then a query can be run/ignored depending on whether a condition is met.

SET @create_table := IF(@tmp >= 1, 'CREATE TABLE table2 SELECT * FROM table1', 'no');

This is usable for a short query like the example above, but not so for more complex queries with multiple statements. Is it possible to do something like this?

IF(@tmp >= 1)
DO SOMETHING
ELSE
DO NOTHING
1
  • 2
    Do not confuse IF() function and IF statement. The function can be used as any other function (your 1st code) whereas the statement can be used in stored object only (procedure, function, trigger, etc.). Commented Nov 11, 2021 at 12:54

1 Answer 1

1

The correct syntax is as follow.

IF condition1 THEN
   {...statements to execute when condition1 is TRUE...}

[ ELSEIF condition2 THEN
   {...statements to execute when condition1 is FALSE and condition2 is TRUE...} ]

[ ELSE
   {...statements to execute when both condition1 and condition2 are FALSE...} ]

END IF;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.