[dba-SQLServer] UPSERT

Arthur Fuller fuller.artful at gmail.com
Tue Jan 29 05:40:54 CST 2008


I just read an interesting piece by Roy Ernest (see
http://www.sqlservercentral.com/articles/T-SQL/61773/) on UPSERT, the
author's term for the fastest way to handle a common problem: if a row
exists, update it; if not, insert it. He shows that the common way of doing
this (I plead guilty) requires two disk hits, when only one is necessary
most of the time.

For details, see the URL above, but the general idea is this: try the update
first, then examine the value of @@rowcount. If it's zero, the row doesn't
exist, so do the insert. I have always checked for the row first, but I now
see the error of my ways.

Arthur



More information about the dba-SQLServer mailing list