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. - IF (1=1) THEN
-
SELECT 'True'
-
ELSE
-
SELECT 'False'
-
END IF
What is wrong? I know it doesn't do anything, but why do I get syntax error?
7 4606
Heya, luttkens. Welcome to TSDN!
Try this instead: -
SELECT
-
IF
-
(
-
{condition},
-
TRUE,
-
FALSE
-
);
-
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.
Thanks!
What if i want to do something like this: -
IF [condition] THEN
-
INSERT INTO ....
-
ELSE
-
UPDATE ....
-
END IF
-
Thanks!
What if i want to do something like this: -
IF [condition] THEN
-
INSERT INTO ....
-
ELSE
-
UPDATE ....
-
END IF
-
I thought pb just explained that to you. Are you looking for the if function instead?
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: - SELECT IF
-
(
-
3>1,
-
INSERT INTO table1(field1) VALUES ('blabla'),
-
INSERT INTO table1(field1) VALUES ('BLABLA')
-
);
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.
Heya, luttkens.
Try this instead: -
INSERT
-
INTO
-
`table1`
-
(
-
`field1`
-
)
-
VALUES
-
(
-
IF(
-
3 > 1,
-
'someText',
-
'someOtherText'
-
)
-
)
-
Thanks for helping me! Problem solved!
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |