user3522145
user3522145

Reputation: 101

use local variables in mysql IF statement

I modified the statements as below, but still errors at the line with the DECLARE statement.

BEGIN DECLARE searchresult int(11); SET searchresult=(Select count(*) from wbsimsynuqsql where SimBase='a cappella'); IF searchresult >0 THEN Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimBase='a cappella') ELSE Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimSyn='a cappella')

END IF; END

I am using the expression below in PHPMYADMIN with the intent to use it later in a PHP/MySQL application. It gives an error relating to the DECLARE statement in line 1.

I've looked at example declarations in MySQL and I don't see an error, but I'm doing something wrong and would appreciate a correction/suggestion.

DECLARE searchresult int(11);
SET searchresult=(Select count(*) from wbsimsynuqsql where SimBase='a cappella');
IF searchresult >0
 {Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimBase='a cappella')}
[ ELSE
   {Select * from simsyn1sql where BaseID = (Select Distinct BaseID from wbsimsynuqsql where SimSyn='a cappella')} ]

END IF;

Upvotes: 1

Views: 3486

Answers (1)

Tin Tran
Tin Tran

Reputation: 6202

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements. like inside a CREATE PROCEDURE you can use DECLARE

Your IF statement is missing a THEN

and remove the square brackets ([]) and the curly braces ({})

If your subquery returns more than one BaseID you might have to use IN instead of =

sqlfiddle

Upvotes: 3

Related Questions