Connecting Tech Pros Worldwide Forums | Help | Site Map

All IF-statement returns "Syntax error"

Newbie
 
Join Date: Jul 2007
Posts: 19
#1: Jul 27 '07
I used to have no problems using IF-statement in SQL-queries. Now nothing works, it always returns "Syntax error". This query also returns an error.

Expand|Select|Wrap|Line Numbers
  1. IF (1=1) THEN
  2.   SELECT 'True'
  3. ELSE
  4.   SELECT 'False'
  5. END IF
What is wrong? I know it doesn't do anything, but why do I get syntax error?



pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Jul 28 '07

re: All IF-statement returns "Syntax error"


Heya, luttkens. Welcome to TSDN!

Try this instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     IF
  3.     (
  4.         {condition},
  5.         TRUE,
  6.         FALSE
  7.     );
  8.  
You can only use IF ... END IF blocks in stored procedures.
http://dev.mysql.com/doc/refman/5.0/...statement.html

P.S., Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Newbie
 
Join Date: Jul 2007
Posts: 19
#3: Jul 29 '07

re: All IF-statement returns "Syntax error"


Thanks!

What if i want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. IF [condition] THEN
  2.      INSERT INTO ....
  3. ELSE
  4.      UPDATE ....
  5. END IF
  6.  
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jul 30 '07

re: All IF-statement returns "Syntax error"


Quote:

Originally Posted by luttkens

Thanks!

What if i want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. IF [condition] THEN
  2.      INSERT INTO ....
  3. ELSE
  4.      UPDATE ....
  5. END IF
  6.  

I thought pb just explained that to you. Are you looking for the if function instead?
Newbie
 
Join Date: Jul 2007
Posts: 19
#5: Aug 1 '07

re: All IF-statement returns "Syntax error"


Quote:

Originally Posted by r035198x

I thought pb just explained that to you. Are you looking for the if function instead?

I don't think so. I tried the code like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT   IF
  2.     (
  3.         3>1,
  4.         INSERT INTO table1(field1) VALUES ('blabla'),
  5.         INSERT INTO table1(field1) VALUES ('BLABLA')
  6.     );
and it still doesn't work. I reckon his code only works if you want to return a string, like you do with the SELECT statement.

What I want to do is checking if a name in a table exists. If false, I want to create a new row and return the ID for the row. If true I just want to return the ID of that existing row.

In SQL-server it works fine just writing like a did in my previous post.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#6: Aug 1 '07

re: All IF-statement returns "Syntax error"


Heya, luttkens.

Try this instead:

Expand|Select|Wrap|Line Numbers
  1. INSERT
  2.     INTO
  3.         `table1`
  4.         (
  5.             `field1`
  6.         )
  7.     VALUES
  8.         (
  9.             IF(
  10.                 3 > 1,
  11.                 'someText',
  12.                 'someOtherText'
  13.             )
  14.         )
  15.  
Newbie
 
Join Date: Jul 2007
Posts: 19
#7: Aug 3 '07

re: All IF-statement returns "Syntax error"


Thanks for helping me! Problem solved!
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#8: Aug 3 '07

re: All IF-statement returns "Syntax error"


Heya, luttkens.

Quote:

Originally Posted by luttkens

Thanks for helping me! Problem solved!

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Reply