2

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

2 Answers2

1

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.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • 1
    you have forgot about `END IF;` please add a tiny info in yours answer :) –  Jul 08 '13 at 06:30
  • also, can you loot at the my comment at the second answer? I just don't want to make copies, thanks! it's about another style –  Jul 08 '13 at 06:32
  • @GeloVolro Thanks to remind. By the I have given the reference site also. but again thanks. – Code Lღver Jul 08 '13 at 06:40
  • http://ideone.com/S7SGI2 May I ask you about smth more? I've done the code, but MySQL is returning not the ALL table data, but strange result: http://s24.postimg.org/dazpic2ht/Untitled.png can you help me, please? thanks! don't be angry, I'm a newbie to SQL procedures :) –  Jul 08 '13 at 06:55
  • @GeloVolro you need to recode the following line as `select COUNT(*) into @tempValue from` and declaire the variable above and then check. – Code Lღver Jul 08 '13 at 07:20
0

You forgot THEN and END IF;

IF @tempValue > 0 THEN 
      SELECT * FROM test.userdata_extended WHERE inn LIKE queryString;
END IF;
mirkobrankovic
  • 2,389
  • 1
  • 21
  • 24
  • Could it be doen with smth similar to: SELECT IF(@tempValue > 0, *, "null") FROM test.userdata_extended WHERE inn LIKE queryString; ? –  Jul 08 '13 at 06:29
  • What do you want to achieve, you need all 3 conditions in if? – mirkobrankovic Jul 08 '13 at 06:36
  • yes, smth similar to that what you've meant, but now I think it's a bad idea, isn't it? –  Jul 08 '13 at 10:21