SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack (2) Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average.
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-17-2006, 02:43 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default SQL 92 dialect question:

Insert or update a record if it already exists?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 04-17-2006, 06:09 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: Insert or update a record if it already exists?

I am afraid there is no sql92 answer to this question.
For sq192 an approach would be to insert every row and use grouping functions for reporting.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 04-17-2006, 06:54 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default MySQL answer. Re: Insert or update a record if it already exists?

MySQL has a special construct for this. Assume the 'username' column below is UNIQUE:

INSERT INTO users (username, email) VALUES ('Jo', 'jo@email.com')
ON DUPLICATE KEY UPDATE email = 'jo@email.com'


The 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-07-2006, 11:20 PM
Rob137
 
Posts: n/a
Default T-SQL answer. Re: Insert or update a record if it already exists?

How about this:
IF (EXISTS (SELECT * FROM AA_TestTable AS t1
  WHERE t1.ord_num = 'FFF'))
begin
  UPDATE AA_TestTable
  SET ord_qty = 999
  WHERE ord_num = 'FFF'
end
else
begin
  INSERT INTO AA_TestTable (ord_num, top_assy, ord_qty)
  VALUES('GGG', 'XYZ', 567)
end 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #5 (permalink)  
Old 09-18-2006, 06:35 AM
srinivas
 
Posts: n/a
Default Oracle answer. Re: Insert or update a record if it already exists?

merge INTO users U1
USING (U1.username = 'Jo' )
when matched then
    UPDATE SET U1.email = 'jo@email.com'
when NOT matched then
     INSERT(U1.email, U1.username)
      VALUES('Jo', jo@email.com);
 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #6 (permalink)  
Old 12-11-2006, 11:44 PM
Evil Overlord
 
Posts: n/a
Default ANY answer. Re: Insert or update a record if it already exists?

INSERT INTO users (username)
SELECT 'Jo'
WHERE 'Jo'
  NOT IN (SELECT username FROM users)

UPDATE users
SET email = 'jo@email.com'
WHERE username = 'Jo'


I use this method a lot. Obviously, 'Jo' would usually either be a variable or a field selected from another table.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #7 (permalink)  
Old 05-07-2007, 04:19 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: Insert or update a record if it already exists?

Quote:
Originally Posted by Rob137 View Post
How about this:
IF (EXISTS (SELECT * FROM AA_TestTable AS t1
  WHERE t1.ord_num = 'FFF'))
begin
  UPDATE AA_TestTable
  SET ord_qty = 999
  WHERE ord_num = 'FFF'
end
else
begin
  INSERT INTO AA_TestTable (ord_num, top_assy, ord_qty)
  VALUES('GGG', 'XYZ', 567)
end 
This opption worked wonders! Thanks for posting
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #8 (permalink)  
Old 05-30-2007, 06:57 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: Insert or update a record if it already exists?

Quote:
Originally Posted by Rob137 View Post
How about this:
IF (EXISTS (SELECT * FROM AA_TestTable AS t1
  WHERE t1.ord_num = 'FFF'))
begin
  UPDATE AA_TestTable
  SET ord_qty = 999
  WHERE ord_num = 'FFF'
end
else
begin
  INSERT INTO AA_TestTable (ord_num, top_assy, ord_qty)
  VALUES('GGG', 'XYZ', 567)
end 
(I assume there is a typo in the last statement, where ord_num should be set to 'FFF').

You should be VERY CAREFUL with things like this. If you can't afford to set the transaction isolation level to SERIALIZABLE, some other transaction could add the row with ord_num='FFF' after you've tested for its existence, but before you've inserted it. This way you end up with violated primary constraint and error in one of these transactions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #9 (permalink)  
Old 10-03-2007, 06:22 PM
mplusch
 
Posts: n/a
Default ANY answer. Re: Insert or update a record if it already exists?

This is a variation that works for tables with multiple primary keys.
If you have a users table with columns of username, dept, and age, and primary keys of username and dept, then this will only insert a user if it doesn't exist already.

INSERT INTO users (username, dept, age) SELECT username='mp', dept='tax', age=5 WHERE (SELECT COUNT (*) FROM users WHERE username='mp' AND dept='tax')=0;

I initially found this syntax confusing, but here's how I 'parsed' it.
This part returns 0 if it doesn't exist:
SELECT COUNT (*) FROM users WHERE username='mp' AND dept='tax'
This part creates a record-like row with static values and represents the record to insert:
SELECT username='mp', dept='tax', age=5
The WHERE-clause following the above part will only return the value to insert if the count is 0.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.sqlrecipes.com/sql_questions_answers/insert_update_record_if_already_exists-6/
Posted By For Type Date
CreativeNRG's bookmarks tagged with This thread Refback 09-03-2007 06:46 AM
Pages tagged with This thread Refback 08-30-2007 07:29 PM


All times are GMT. The time now is 09:16 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7