473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

All IF-statement returns "Syntax error"

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
7 4606
pbmods
5,821 Expert 4TB
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
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
r035198x
13,262 8TB
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
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
5,821 Expert 4TB
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
Thanks for helping me! Problem solved!
Aug 3 '07 #7
pbmods
5,821 Expert 4TB
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

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

Similar topics

3
by: dan glenn | last post by:
hi. I want to code a 'preview' function into a guestbook entry page. I can do it with a button that posts, bringing up a whole new page showing a preview of what has been entered, and then the user...
5
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of...
23
by: rithish | last post by:
<code> var dt = new Date(2004, 0, 1); alert ( dt.getDate() ); // returns proper date alert ( dt.getUTCFullYear() ); // returns 2003 </code> Why is this so? Or is that I am missing...
4
by: ezra epstein | last post by:
Aother head banger for me. Below is a complete example of the code Using Postgres 7.4, the function "test" gets this: psql:temp3.sql:10: ERROR: syntax error at or near "%" at character 135...
1
by: Morten Nielsen | last post by:
Does anyone have any idea why I can't get a pixel-value of 255 returned in the code below? Any color-value from 0 up to 254 is returned correctly, but any color-component of 255 returns zero. ie....
5
by: Susan Geller | last post by:
Character returns stored in a table do not display in a textbox or on a datagrid on my .net form. The text displays without the returns making the text difficult to read. When I "view source" on...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
17
by: nergal | last post by:
What is a good programming style in C to handle multiple returns in a function that returns different values? - To have a variable that is set to the return value, which is in the end of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.