What is the preferred merge method for SQL Server 2005?

I mainly used the Exists method to join a row into a table, but I am considering switching to the Row Counting Method . Is there any reason not to do this?

There is a method

If Exists(Select * From Table Where ID = @ID) Begin

    Update Table Set Value = @Value Where ID = @ID  

End Else Begin

    Insert Into Table (Value) Values (@Value);      

End


Row Count Method

Update Table Set Value = @Value Where ID = @ID 

If (@@RowCount = 0) Begin

    Insert Into Table (Value) Values (@Value);      

End


Performance

The line counting method seems much faster. On a table with approximately 50 thousand lines, it is synchronized in 1/5 of the time. There is a method . The tests were not very scientific, but even with a conservative +/- 15%, which is important. This is the main reason I want to switch.


Note

Examples have been specially created for readability. They in no way reflect my actual situation.

+4
3

. , , ( ). , , .

, SQL Server 2008 MERGE ( , ).

+3

.

+1

The biggest reason not to switch is that what works for you now and making changes introduces the possibility of new errors. If you want to change when you update other things, that's fine, but what are you really going to do? get from change? I suspect that performance gains if any of them are likely to be very small, as the examples you give seem to use separate entries.

+1
source

All Articles