MySQL Forums
Forum List  »  General

IF THEN ELSE, not working, but why?
Posted by: Lyle Hopkins
Date: July 29, 2008 09:44AM

I'm trying to get IF and ELSE statements to work. I've written:-

SET AUTOCOMMIT = 0;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN;
SELECT @ip := `ip` FROM `ip_list` WHERE `ip`='1.1.1.1';
IF @ip != '1.1.1.1' THEN
INSERT INTO `ip_list` ( `ip` ) VALUES( '1.1.1.1' );
END IF;
COMMIT;

I get the normal error ".... for the right syntax to use near 'IF @ip...';


Why isn't this working? I know that this example is similar to ON DUPLICATE KEY UPDATE, but I don't want to use that. Why doesn't mysql like my if? The documents page leaves a little to be desired:-
http://dev.mysql.com/doc/refman/5.0/en/if-statement.html


Lyle

P.S. Update, this does not work either:-

SET AUTOCOMMIT = 0;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN;
SELECT @ip := `ip` FROM `ip_list` WHERE `ip`='1.1.1.1';
INSERT INTO `ip_list` ( `ip` ) VALUES( '1.1.1.1' ) IF ( @ip != '1.1.1.1' );
RETURN @ip;
COMMIT;



Edited 2 time(s). Last edit at 07/29/2008 10:02AM by Lyle Hopkins.

Options: ReplyQuote


Subject
Written By
Posted
IF THEN ELSE, not working, but why?
July 29, 2008 09:44AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.