I have a table in Microsoft SQL Server. Sometimes I need to update, and sometimes I need to insert. I could write 2 stored procedures:
InsertNewPerson
UpdatePertsonById
But I was thinking to write 1 stored procedure instead (SetPerson
) which would do both (if there is an ID
, it is an update
operation , else insert
).
Should I create one stored procedure (only one to maintain) or should I create two different stored procedures?
Best Answer
As far as I understand it you are not actually talking about an
UPSERT
here just combining two different CRUD operations in one stored procedure.The benefit I see to this is that you don't have to maintain two separate parameter lists if the table structure changes. The disadvantage is that the single stored procedure now has two responsibilities and is somewhat less easy to understand.
I would generally opt for separating them into two stored procedures.
RE: "Can you elaborate how an upsert will look"
This assumes that
Id
is no longer anIDENTITY
column. The reason for usingHOLDLOCK
is explained here.