Creating an update Stored Procedure that does not change the fields when a the corresponding parameter is not passed in

I would like to update a record in SQL Server using a Stored Procedure. If the parameter passed in not passed in I do not want that field corisponding to the parameter updated. What is a good way to program this?

I'm hoping I don't have to write something like:
 
 ALTER PROCEDURE dbo.MyTableU
  (
    @IDParameter int,
    @parameter1 int = NULL,
    @parameter2 tinyint = NULL,
    @parameter3 smalldatetime = NULL
   )
AS
  IF (@parameter1 IS NULL AND NOT @parameter2 IS NULL AND NOT @parameter3)
   BEGIN
     UPDATE MyTable SET Value2 = @parameter2, Value3 = @parameter3
      WHERE ID = @IDParameter
   END
       
and so on.... for every combination.
r1goodwinAsked:
Who is Participating?
 
rafranciscoConnect With a Mentor Commented:
Try this:

UPDATE MyTable
SET Value2 = ISNULL(@parameter2, Value2),
       Value3 = ISNULL(@parameter3, Value3)
WHERE ID = @IDParameter
0
 
rafranciscoCommented:
I forgot the first parameter:

UPDATE MyTable
SET Value1 = ISNULL(@parameter1, Value1),
       Value2 = ISNULL(@parameter2, Value2),
       Value3 = ISNULL(@parameter3, Value3)
WHERE ID = @IDParameter
0
 
r1goodwinAuthor Commented:
Thanks! This is exactly what I was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.