By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

All IF-statement returns "Syntax error"

P: 23
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?
Jul 27 '07 #1
Share this Question
Share on Google+
7 Replies


pbmods
Expert 5K+
P: 5,821
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.
Jul 28 '07 #2

P: 23
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.  
Jul 29 '07 #3

10K+
P: 13,264
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?
Jul 30 '07 #4

P: 23
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.
Aug 1 '07 #5

pbmods
Expert 5K+
P: 5,821
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.  
Aug 1 '07 #6

P: 23
Thanks for helping me! Problem solved!
Aug 3 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, 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 :)
Aug 3 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.