I want to be able to pass in a list of parameters, and ignore the ones which are NULL. So that the query is in effect pretending that the filter isn\'t there and ignoring it
A technique I’ve used in the past for this scenario is to utilize the COALESCE function as part of my WHERE clause. Books Online will provide more in depth info on the function, but here’s a snippet of how you can use it in the scenario you described:
create procedure usp_TEST_COALESCE
(
@parm1 varchar(32) = null,
@parm2 varchar(32) = null,
@parm3 int = null
)
AS
SELECT *
FROM [TableName]
WHERE Field1 = COALESCE(@parm1, Field1)
AND Field2 = COALESCE(@parm2, Field2)
AND Field3 = COALESCE(@parm3, Field3)
The COALESCE function will return the first non-null expression from its arguments. In the example above, if any of the parameters are null, the COALESCE function will use the value in the underlying field.
One important caveat to using this technique is that the underlying fields in the table (that make up your where clause) need to be non-nullable.
when you declare the parameters if you set a value to them such as null in your case you do not need to pass a value in to them unless of course you need to. I use this ability to flag if another query needs to be run is special cases when the parameter is not null
I typically just check it like this
IF field IS NULL
Look at the following link in the section titled "The Case Study: Searching Orders". This explores all options in depth and should give you an excellent overview of the costs associated with each of these options. Warning, be very careful when using COALESCE it may not return what you think it is.
Regards,
Tim
If the Thing (column value) is also Nullable, then use the foll. approach:
WHERE COALESCE(Thing,'')=COALESCE(@thing,Thing,'')
This is the method I typically use. I see no reason for it to be inefficient, as the statement should short-circuit to true if @thing is null, and would therefore not require a table scan. Do you have any evidence that this comparison is slowing your query? If not, I would not worry about it.
Thanks, This was helpful. I have decided to use the sp_ExecuteSQL method due to the potential performance advantages mentioned. I have a slightly different take on it which you may find helpful.
DECLARE @sql nvarchar(4000)
DECLARE @where nvarchar(1000) =''
SET @sql = 'SELECT * FROM MyTable'
IF @Param1 IS NOT NULL
SET @where = @where + ' AND Field1 = @Param1'
IF @Param2 IS NOT NULL
SET @where = @where + ' AND Field2 = @Param2'
IF @Param3 IS NOT NULL
SET @where = @where + ' AND Field3 = @Param3'
-- Add WHERE if where clause exists, 1=1 is included because @where begins with AND
IF @where <> ''
SET @sql = @sql + ' WHERE 1=1' + @where
--Note that we could also create order parameters and append here
SET @sql = @sql + ' ORDER BY Field1'