T-SQL Insert or update

Both work fine, but I usually use option 2 (pre-mssql 2008) since it reads a bit more clearly. I wouldn’t stress about the performance here either…If it becomes an issue, you can use NOLOCK in the exists clause. Though before you start using NOLOCK everywhere, make sure you’ve covered all your bases (indexes and big picture architecture stuff). If you know you will be updating every item more than once, then it might pay to consider option 1.

Option 3 is to not use destructive updates. It takes more work, but basically you insert a new row every time the data changes (never update or delete from the table) and have a view that selects all the most recent rows. It’s useful if you want the table to contain a history of all its previous states, but it can also be overkill.

Leave a Comment