Hi folks,
I've got a classic case of UPDATE or INSERTing some data into a table. I'm not sure if I should just do an UPDATE and if i get zero ROWCOUNT, then do an INSERT. Alternatively, I've heard rumours that the MERGE statement now replaces this, but I'm not sure how and if it's appropriate, in this situation.
Here's some sample sql to help demonstrate this...
ALTER PROCEDURE [dbo].[InsertLocationName]
(
@SomeId INTEGER,
@SomeName NVARCHAR(100)
)
AS
BEGIN
SET NOCOUNT ON
UPDATE TableFoo
SET SomeName = @SomeName
WHERE SomeId = @SomeId
-- Did we update something?
IF @@ROWCOUNT <= 0
-- Nope, so add the record.
INSERT INTO TableFoo
VALUES (@SomeName)
END
thoughts?