Solved

Insert if not exists else update mysql

Posted on 2010-09-22
8
1,550 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
  • 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
 
LVL 3

Author Comment

by:bretterer
ID: 33738925
Error Code : 1327
Undeclared variable: cnt
(0 ms taken)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now