<p>I'm trying to do the classic Insert/Update scenario where I need to update existing rows in a database or insert them if they are not there.</p>

<p>I've found a <a href="https://stackoverflow.com/questions/13540/insert-update-stored-proc-on-sql-server">previous question on the subject</a>, but it deals with stored procedures, which I'm not using.  I'd like to just use plain SQL SELECT, INSERT and UPDATE statements, unless there's something better available (the MERGE statement isn't available in SQL Server 2005).</p>

<p>I guess my general idea is this:</p>

<pre><code>If the row is found
  update
else
  insert
</code></pre>

<p>As for checking for a row's existence, how expensive is it to do a SELECT statement before calling an UPDATE or an INSERT?  Or is it better to just try an UPDATE, check for the number of rows affected, and then do an INSERT if the rows affected is 0?</p>

The most efficient way is to do the
```
UPDATE
```
, then do an
```
INSERT
```
if
```
@@rowcount
```
is zero,
as explained in this previous answer
.