VB icon

How to use ON DUPLICATE KEY in insert statement

Email
Submitted on: 11/19/2015 2:59:52 AM
By: Amit Gaur  
Level: Intermediate
User Rating: Unrated
Compatibility: SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier, Informix , Oracle, Other
Views: 11949
 
     How to use ON DUPLICATE KEY in insert statement. In case of inserting or updating record we use separate logic using if statement or case statement: e.g.: if(i==0) insert here if(i=1) update here we can terminate the above if statement by using On Duplicate Key. by using this only one statement can be use for insert and update record. *note: user_nm coulmn must be unique Find more different interesting code and free software at : http://agalaxycode.blogspot.com
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
--**************************************
-- Name: How to use ON DUPLICATE KEY in insert statement
-- Description:How to use ON DUPLICATE KEY in insert statement.
In case of inserting or updating record we use separate logic using if statement or case statement:
e.g.:
if(i==0)
insert here
if(i=1)
update here
we can terminate the above if statement by using On Duplicate Key.
by using this only one statement can be use for insert and update record.
*note: user_nm coulmn must be unique
Find more different interesting code and free software at : 
http://agalaxycode.blogspot.com

-- By: Amit Gaur
--**************************************

INSERT INTO `posts` (`user_nm`, `no_of_update`, `inserted_on`, `update_on`)
VALUES ('a_user', 1, NOW(), NOW())
ON DUPLICATE KEY
UPDATE `no_of_update` = `no_of_update` + 1, `update_on` = NOW()


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.