IF condition inside a stored procedure

I get an error pointing to the if loop when I am creating this stored procedure. Is there any other method to use the conditional and what is the syntactical error that I am making??
delimiter //

CREATE DEFINER=`root`@`localhost` PROCEDURE `getData1`(IN templateName VARCHAR(45),IN templateVersion VARCHAR(45),IN userId VARCHAR(45))
BEGIN
	
	set @version = CONCAT("SELECT 'saveOEMsData_answersVersion' FROM `saveOEMsData` where saveOEMsData_templateName = '",templateName,"' and saveOEMsData_templateVersion = ",templateVersion," and saveOEMsData_userId= ",userId," GROUP BY `saveOEMsData_templateName`"); 
	PREPARE s1 from @version;
	EXECUTE S1;
	IF(@version==1) THEN select 'one';
	ELSE select 'zero';
	END IF;
END
//
delimiter ;

Open in new window

gautam_reddycAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RosenetCommented:
SQL Server condition syntax for "equals" is not "==", just use a single "=".

IF(@version==1) THEN select 'one';

should be

IF(@version=1) THEN select 'one';
0
gautam_reddycAuthor Commented:
hey rosenet!! I am able to compile the procedure, but there seems to be a confusion. The variable @version holds the whole query statement and not just the value of version.

How do only retrieve the version so that I can compare using the IF conditional?
0
RosenetCommented:
Oh completely my fault I didn't realize you were working with MySQL I gave you an answer for SQL Server.
Sorry for the confusion I don't think I can help you.
0
SoLostCommented:
I believe you have the syntax incorrect.

Try the following :
delimiter //

CREATE DEFINER=`root`@`localhost` PROCEDURE `getData1`(IN sTemplateName VARCHAR(45),IN sTemplateVersion VARCHAR(45),IN sUserId VARCHAR(45))
BEGIN
  DECLARE iVersion INT DEFAULT 0;

  /* Store the version number into iVersion */
  SELECT saveOEMsData_answersVersion INTO iVersion
    FROM saveOEMsData
    WHERE saveOEMsData_templateName = sTemplateName
    AND saveOEMsData_templateVersion = sTemplateVersion
    AND saveOEMsData_userId= sUserId
    GROUP BY saveOEMsData_templateName;

  /* Return the result */
  SELECT IF(iVersion=1, 'one', 'zero');

END
//
delimiter ;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gautam_reddycAuthor Commented:
Thanks soLost & rosenet!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.