user2402179
user2402179

Reputation:

If-statement in the MySQL stored procedure for selecting data

I have the next SQL code:

DELIMITER $$

CREATE PROCEDURE `test`.`new_procedure` (queryString VARCHAR(255))
BEGIN
    SELECT @tempValue = COUNT(*) FROM test.userdata_extended WHERE inn LIKE queryString;
    IF @tempValue > 0 SELECT * FROM test.userdata_extended WHERE inn LIKE queryString;
END $$

I'm putting the COUNT* result into @tempValue variable. Then I'm trying to compare it for being greater than zero.

I'm getting error with the statement comparing process. MySQL reports me a UNEXPECTED SELECT SYM error. What does it mean?

Also this would be a check for another tables. I need IF-ELSE statements, because basing on several query result my procedure must return the exact code error value (which could be different) for the my developed application to handle or giving the data, if all is fine.

How to fix this issue?

Thanks

Upvotes: 2

Views: 2746

Answers (2)

Code Lღver
Code Lღver

Reputation: 15603

You have forgot the THEN in your if statement. You need to add the THEN.

Like this:

IF @tempValue > 0 THEN ...your statement 
END IF;

You have also forgot to add the END IF; add this also.

Reference site is here.

Upvotes: 1

mirkobrankovic
mirkobrankovic

Reputation: 2347

You forgot THEN and END IF;

IF @tempValue > 0 THEN 
      SELECT * FROM test.userdata_extended WHERE inn LIKE queryString;
END IF;

Upvotes: 0

Related Questions