0

Wherever I find an example of IF () function I only see something like below.

SELECT IF(500<1000, "YES", "NO");
+---------------------------+
| IF(500<1000, "YES", "NO") |
+---------------------------+
| YES                       |
+---------------------------+

But can I do something like below i.e executing a query?

IF(500<1000, CREATE DATABASE test1; , CREATE DATABASE test2;);

One way is to using prepared statements. But I want to directly execute the query inside it.

9
  • SELECT IF (500<1000, 1, 0)
    – McNets
    Jan 28, 2019 at 15:56
  • Actually I need to execute sql query. Not any string or integer.
    – SkyRar
    Jan 28, 2019 at 16:08
  • I have updated my question.
    – SkyRar
    Jan 28, 2019 at 16:19
  • 1
    You need to use a statement block like CASE, not a function. Jan 28, 2019 at 17:46
  • 1
    @SkyRar There is an IF() function and IF..THEN..ELSE..END IF; statements. You can call another statements from the IF statement not from the IF() function.
    – Kondybas
    Jan 28, 2019 at 18:24

1 Answer 1

1

Learn about Stored Programs. Write a Procedure that uses IF statements, not functions, to conditionally perform SQL statements.

Then invoke the Stored Procedure with a CALL statement.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.