I have started creating a stored procedure that will search through my database table based on the passed parameters. So far I already heard about potential problems with kitchen sink parameter sniffing
. There are a few articles that helped understand the problem but I'm still not 100% that I have a good solution. I have a few screens in the system that will search different tables in my database. All of them have three different criteria that the user will select and search on. First criteria are Status
that can be Active
,Inactive
or All
. Next will be Filter By
, this can offer different options to the user depends on the table and the number of columns. Usually, users can select to filter by Name
,Code
,Number
,DOB
,Email
,UserName
or Show All
. Each search screen will have at least 3 filters and one of them will be Show All
. I have created a stored procedure where the user can search Status
and Filter By
Name
,Code
or Show All
. One problem that I have is Status
filter. Seems that SQL will check all options in where clause so If I pass parameter 1
SP returns all active records if I pass 0
then only inactive records. The problem is if I pass 2
SP should return all records (active and inactive) but I see only active records. Here is an example:
USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[Search_Master]
@Status BIT = NULL,
@FilterBy INT = NULL,
@Name VARCHAR(50) = NULL,
@Code CHAR(2) = NULL
WITH RECOMPILE
AS
DECLARE @MasterStatus INT;
DECLARE @MasterFilter INT;
DECLARE @MasterName VARCHAR(50);
DECLARE @MasterCode CHAR(2);
SET @MasterStatus = @Status;
SET @MasterFilter = @FilterBy;
SET @MasterName = @Name;
SET @MasterCode = @Code;
SELECT RecID, Status, Code, Name
FROM Master
WHERE
(
(@MasterFilter = 1 AND Name LIKE '%'+@MasterName+'%')
OR
(@MasterFilter = 2 AND Code = @MasterCode)
OR
(@MasterFilter = 3 AND @MasterName IS NULL AND @MasterCode IS NULL)
)
AND
(
(@MasterStatus != 2 AND MasterStatus = @Status)
OR
(@MasterStatus = 2 AND 1=1)
);
Other than problem with Status filter I'm wondering if there is any other issues that I might have with parameter sniffing? I found a blog that talks about preventing sniffing and one way to do that is by declaring local variables. If anyone have suggestions or solution for Status filter please let me know.