SQL Server: Stored Procedure for update with optional parameters

Problem:

One more article in Problem-Solution approach to keep it short and simple.

When we are writing a stored procedure to update different fields of a table for one or more records, we may write as the below script.

CREATE PROCEDURE [dbo].[UpdtEmployee]
(
@EmpId INT,
@EmpName VARCHAR(100)=NULL,
@Address VARCHAR(200)=NULL,
@EmpPhotoPath VARCHAR(100)=NULL,
@MobileNo VARCHAR(14)=NULL
)
AS
BEGIN
UPDATE dbo.Employee
SET EmpName=@EmpName,Address=@Address,EmpPhotoPath=@EmpPhotoPath,MobileNo=@MobileNo
WHERE EmpId=@EmpId
END

The problem with the above stored procedure is when we want to update only those fields/columns of the record for which corresponding parameter is not having NULL values then it will not yield the anticipated result. In this scenario this stored procedure will update all the fields with the values from corresponding parameters. Let’s take an example so that the problem can be understood clearly.

Example: Let’s assume that we have inserted values to all the fields of a record. Now we want to modify only the mobile number of that employee. If we call this procedure then it will make values of all other fields to NULL as we have declared the default value for all the parameters as NULL.

Solution:

Solution to this problem is to avoid updating those fields which have corresponding parameters with NULL values or, we can update existing data corresponding to those fields. We will go for the later one. Now, the modified script may look like


CREATE PROCEDURE [dbo].[UpdtEmployee]
(
 @EmpId INT,
 @EmpName VARCHAR(100)=NULL,
 @Address VARCHAR(200)=NULL,
 @EmpPhotoPath VARCHAR(100)=NULL,
 @MobileNo VARCHAR(14)=NULL
)
AS
BEGINd
 UPDATE dbo.Employee
 SET EmpName=ISNULL(@EmpName,EmpName),Address=ISNULL(@Address,Address),EmpPhotoPath=ISNULL(@EmpPhotoPath,EmpPhotoPath),MobileNo=ISNULL(@MobileNo,MobileNo)
 WHERE EmpId=@EmpId
END

Hopefully this article may be useful to you.

Thanks !!!

About these ads

About Suvendu

My focused area of learning are ASP.Net, SQL Server, Javascript, Telerik, HTML, CSS ,MVC architecture etc. My areas of interest includes Windows Phone development, Silverlight etc. Blogging and sharing knowledge is my passion. I blog regularly at https://suvendugiri.wordpress.com/

Posted on March 22, 2012, in Problem-Solution, SQL Server, Stored Procedure and tagged , . Bookmark the permalink. 6 Comments.

  1. Thanks for sharing!! It helped me a lot!! ;]
    Cheers.

  2. thanks dud..thanks for sharing..it helped me a lot…

  3. thnx for the explain so eaisly. so plz 1 reqst for you explain trigger with example for update & instead of trigger. i would b thankful to you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: