Skip to content
Advertisement

Mysql 8 How to use an if inside a stored procedure and using cte

I am using a CTE inside a stored procedure, and it works ok, but the problem is when I try to add a conditional, I got the following message:

You have an error in your sql ..
Syntax to use near ‘IF.

Advertisement

Answer

You cannot use an IF statement in a query. It is meant to execute a block of SQL code depending on a condition, not to be used within a query.

I think, however, that you could rephrase your query as follows:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement