Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
How do you Insert *OR* Update in SQL?
Old 02-01-2008, 06:08 PM How do you Insert *OR* Update in SQL?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
This isn't a question where I'm too lazy to look up the answer myself. Here's a sample script:

Code:
If Exists (Select A_Small_Field From MyTable Where Record_ID = ?)
   Update MyTable Set X = ?, Y = ?, Z = ? Where Record_ID = ?
Else
   Insert Into MyTable Values ( ?, ?, ?, ?, ? )
But no matter what happens, you run a very similar query twice. If the system isn't hammered, and the record exists, SQL should cache the row when you run the exists subquery. So it should at least update against memory instead of against the disc. Still, it will have to find that row twice.

Is this really the best way? I mean in terms of performance, I don't mind if it means extra code.

I was thinking I could use a unique constraint with ignore_dup_keys set to on, except that I don't need to just verify a record exists, I actually need to change some of its values.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-14-2008, 11:55 AM Re: How do you Insert *OR* Update in SQL?
Novice Talker

Posts: 5
Trades: 0
No matter what you insert or update, you will need first to find the record where the information to modify is needed.
ludmila is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How do you Insert *OR* Update in SQL?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42103 seconds with 12 queries