Using UPDATE in stored procedure with optional parameters
Try this.
ALTER PROCEDURE [dbo].[sp_ClientNotes_update]
@id uniqueidentifier,
@ordering smallint = NULL,
@title nvarchar(20) = NULL,
@content text = NULL
AS
BEGIN
SET NOCOUNT ON;
UPDATE tbl_ClientNotes
SET ordering=ISNULL(@ordering,ordering),
title=ISNULL(@title,title),
content=ISNULL(@content, content)
WHERE id=@id
END
It might also be worth adding an extra part to the WHERE
clause, if you use transactional replication then it will send another update to the subscriber if all are NULL, to prevent this.
WHERE id=@id AND (@ordering IS NOT NULL OR
@title IS NOT NULL OR
@content IS NOT NULL)
One Idea:
UPDATE tbl_ClientNotes
SET ordering=ISNULL(@ordering, ordering),
title=ISNULL(@title, title),
content=ISNULL(@content, content)
WHERE id=@id
UPDATE tbl_ClientNotes
SET
ordering=ISNULL@ordering,ordering),
title=isnull(@title,title),
content=isnull(@content,content)
WHERE id=@id
I think I remember seeing before that if you are updating to the same value SQL Server will actually recognize this and won't do an unnecessary write.