Reputation: 19443
I want to do something like
IF (EXISTS (select * from Table1 where <Condition>))
{
INSERT INTO Table2 ...
}
ELSE
{
INSERT INTO Table3...
<Do some more manipulations>
}
AFAIK I can only use the IF
as part of a statement.
Is there a way to do it?
Thanks,
==Edit 1==
I would like to do this logic as part of a Stored Procedure.
Upvotes: 0
Views: 106
Reputation: 29051
Try this:
IF EXISTS (SELECT * FROM Table1 WHERE <CONDITION>) THEN
BEGIN
INSERT INTO Table2 ...
END
ELSE
BEGIN
INSERT INTO Table3...
<DO SOME more manipulations>
END
END IF
Check IF Statement in MySQL
Upvotes: 1
Reputation: 24002
The only way is by calling a stored procedure.
Using stored procedure, you can define variables, check multiple conditions, execute multiple statements, etc.
But, just with IFEXISTS
and ELSE
at max you can execute 2 statements in straight.
References: MySQL: CREATE PROCEDURE Syntax
Upvotes: 0