From time to time my stored procedures looks like
create procedure handle_data
@fk int,
@value varchar(10)
as
begin
if exists (select * from my_table where id = @fk)
begin
update my_table
set value = @value
where id = @fk
end
else
begin
insert into my_table (fk, value)
select @fk, @value
end
end
Probably there is fault in design of app which calls this stored procedure.
Should I avoid making apps which do same stored procedure and methods for inserting new data and also updating old ones?
Is there better way to achieve updating or inserting data in one approach?
I am using SQL Server 2005 / 2008.
Best Answer
A slightly more efficient way (which will do at worst one seek/scan instead of two against the existing data):
MERGE
may be tempting, however there are a few reasons I shy away from it.MERGE
and probably many more that have yet to be uncovered