views:

357

answers:

1

I am attempting to run the following code in phpMyAdmin:

set @tagTokenNum=0;
select @tagTokenNum:=tag_token_id from tag_token where tag_token_name = 'XXX';
delimiter go
if @tagTokenNum is null then 
   insert into tag_token (tag_token_name) values ('XXX');
   select @tagTokenNum:=last_insert_id();
end if;
go

I have also tried this without the delimiter statement and corresponding go. I receive the error message "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if @tagTokenNum is null then insert into tag_token ..."

As far as I can tell, I am following proper syntax for IF statements in MySql. I'm much more familiar with Microsoft SQL, so it's entirely possible I'm doing something wrong. I'd appreciate any help you can offer. Thanks. - Dave

A: 

ISTR that you can only use if statements in stored procedures, ref: mysql docs ...An IF ... END IF block, like all other flow-control blocks used within stored programs....

However I think that you could achieve the same with the following:

insert into tag_token (tag_token_name) values ('XXX')
    ON DUPLICATE KEY UPDATE tag_token_name = tag_token_name;

select @tagTokenNum:=tag_token_id from tag_token where tag_token_name = 'XXX'
Richard Harrison
I haven't verified the workaround, but he's definitely right on IF statements ...
davearchie