Solved

Insert if not exists else update mysql

Posted on 2010-09-22
8
1,574 Views
Last Modified: 2012-05-10
Hello.  I have been looking for tihs everywhere.  im trying to run this query

IF NOT EXISTS (SELECT * FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid='1') THEN
	INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
ELSE
	UPDATE tutorials SET keywordlist = "HELLO WORLD" WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid = '1'

Open in new window


this is giving me the error

Error Code : 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 NOT EXISTS (SELECT * FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND ' at line 1

and i cant find out what is wrong with the syntax.

For reasons i dont want to go into i can not use anything that uses duplicate key to accomplish this or the REPLACE

Thank You!
0
Comment
Question by:bretterer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33738708
MySql has something called on duplicate which you can use

basically insert the record and if duplicate key error occurs then perform the update

INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
ON DUPLICATE KEY UPDATE SET keywordlist = "HELLO WORLD"

Its something along those lines anyways. your dup key index would need to be setup correctly I guess

backup table/db just in case it didnt quite go according to plan
0
 
LVL 3

Author Comment

by:bretterer
ID: 33738737
rockiroads,  Thankyou for your quick response but let me point out what i said before

and i quote
" i can not use anything that uses duplicate key to accomplish this"
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 33738888
ok, sorry.

well only other thing is perhaps a stored procedure within mysql or use some other app to run mysql commands eg vb/ado

mysql stored procedure - something along these lines, cant remember exact syntax
try maybe use a variable then check that (eg cnt)


create procedure myproc
begin
    SELECT count(*) into cnt FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid='1'
    if cnt = 0 then
        INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
    else
        UPDATE tutorials SET keywordlist = "HELLO WORLD" WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid = '1'
    end if
end
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:bretterer
ID: 33738925
Error Code : 1327
Undeclared variable: cnt
(0 ms taken)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33739635
can u try defining the variable, after begin, before select

DECLARE cnt INT DEFAULT 0
0
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 250 total points
ID: 33743427
Try this.

REPLACE INTO `tutorials`
SET `tutorialid` = 'Alg1_01_02_0001',
`keywordlist` = 'Hello World',
`subjectid` = '1';

got from
http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html
0
 
LVL 3

Author Comment

by:bretterer
ID: 33745168
Brichsoft.  That did do it although i said i didnt want to use replace.   becuase of database structure.  I did modify database and it worked.  
0
 
LVL 3

Author Closing Comment

by:bretterer
ID: 33745193
Thanks
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question