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 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.
Let’s start creating stored procedure with a very simple one and then we will analyse it line by line.
DELIMITER $$ DROP PROCEDURE IF EXISTS MyFirstSP$$ CREATE PROCEDURE MyFirstSP() BEGIN SELECT * FROM MyTable; END$$
- By default MySQL treats semicolon(;) as the statement terminator or end of statement. But as we are going to use it inside the procedure body, so we need another different delimiter to state the end of the stored procedure. DELIMITER $$ sets $$ as the statement terminator.
- The DROP PROCEDURE IF EXISTS <SPName> statement checks for a duplicate stored procedure with the same name and if there exists any then issue a DROP command. You can skip this line if you are sure that there is no other stored procedure exists with the same name in your selected database.
- Statement CREATE PROCEDURE marks the start of the stored procedure definition. Here, MyFirstSP is the name of our stored procedure. The stored procedure name followed by a pair of parentheses. The use of these parentheses is to define parameters inside it. In this stored procedure we don’t need any parameters, but we have to put these parentheses as this is mandatory in MySQL unlike SQL Server.
- The BEGIN Statement marks the start or begining of a block (here the block is the stored procedure itself).In stored procedures, every statements with multiple statements should be enclosed with a block defined by BEGIN and END, where END statement marks the end of the block;
- The statement inside the BEGIN .. END is a simple SELECT query, which fetches all the records from MyTable table.
I took the Problem-Solution approach to write this post to keep it short and simple.
Here the problem I am talking about is the scenario when you have to write a stored procedure in SQL Server with multiple search options/parameters then you may use some IF…ELSE .. statements to do it. Let’s take an short example.
CREATE PROCEDURE [dbo].[getEmployeeList] ( @DeptId INT=NULL, @OfficeId INT=NULL ) AS BEGIN IF(@DeptId IS NOT NULL AND @OfficeId IS NOT NULL) BEGIN SELECT * FROM dbo.Employees WHERE DeptId=@DeptId AND OfficeId=@OfficeId END ELSE IF(@DeptId IS NOT NULL AND @OfficeId IS NULL) BEGIN SELECT * FROM dbo.Employees WHERE DeptId=@DeptId END ELSE IF(@DeptId IS NULL AND @OfficeId IS NOT NULL) BEGIN SELECT * FROM dbo.Employees WHERE OfficeId=@OfficeId END ELSE BEGIN SELECT * FROM dbo.Employees END END
Here we can have 4 combinations of search options.
Now, if we want add some more parameters like SectionId,DesignationId,CityId etc. to this stored procedure then it will be a tough task to manage it and may end-up with a 100′s lines of script.
I found a solution for this problem by creating multiple conditions for a single WHERE… tag. Now, if I implement it to the previous example then the stored procedure will be like the below script-
CREATE PROCEDURE [dbo].[getEmployeeList] ( @DeptId INT=NULL, @OfficeId INT=NULL ) AS BEGIN SELECT * FROM dbo.Employees WHERE (@DeptId IS NULL OR DeptId=@DeptId) AND (@OfficeId IS NULL OR OfficeId=@OfficeId) END
Just add conditions like these two to the WHERE part, as many as optional parameters you have in your stored procedure.
I hope it will be helpful to those who are looking a solution for the above said problem. Please share your thoughts and ideas regarding this post.