I have a big SPROC which performs a search with a CTE depending on the value of a parameter being passed into the SPROC.
The CTE is used for paging purposes (SQL Server 2008).
I am wondering if there is a way to modify the query chain so that if a 2nd parameter is passed, it is computed as an optional parameter and bring back results with that parameter in mind?
so instead of doing a check to see if the 2nd parameter is null, then do this kind of search otherwise do another kind of search - is there a way I can include the 2nd parameter in the existing query but if it is not null then to AND it?
a typical query I am performing is as follows:
WITH ProductSummary AS
	(
		SELECT p.[ID], p.Product, p.SecondName, p.Manufacturer, p.Category, p.CategoryID,
		ROW_NUMBER() OVER (ORDER BY 
		CASE @sortDir
			WHEN 'DESC' THEN NULL ELSE 1
		END ASC,		
		CASE @sortByFieldName
			WHEN 'Product' THEN p.Product
			WHEN 'CommonName' THEN p.SECONDNAME
			WHEN 'Manufacturer' THEN p.MANUFACTURER
			WHEN 'Category' THEN p.Category
		END ASC,
		CASE @sortByFieldName
			WHEN 'SDSID' THEN p.SDSID --CAST(SDSID as varchar(50))		
		END ASC) AS RowNumberAsc,
		ROW_NUMBER() OVER (ORDER BY
		CASE @sortDir
			WHEN 'ASC' THEN NULL ELSE 1 
		END DESC,	
			
		CASE @sortByFieldName			
			WHEN 'Product' THEN p.Product
			WHEN 'CommonName' THEN p.SECONDNAME
			WHEN 'Manufacturer' THEN p.MANUFACTURER
			WHEN 'Category' THEN p.Category
		END DESC,
		
		CASE @sortByFieldName 
			WHEN 'ID' THEN p.ID -- CAST(SDSID as varchar(50))
		END DESC) AS RowNumberDesc
		
		
		FROM v_ProdsSummary p
		WHERE (@columnName IS NULL AND @columnValue IS NULL)
		OR
			CASE @columnName
				WHEN 'Category' THEN p.CATEGORY
				WHEN 'Manufacturer' THEN p.MANUFACTURER
				WHEN 'Manufacturer Name' THEN p.MANUFACTURER
				WHEN 'Second Name' THEN p.SECONDNAME
				WHEN 'Common Name' THEN p.SECONDNAME
				WHEN 'Product Name' THEN p.PRODUCT				
			END
		LIKE @columnValue
	)
	
	
		SELECT 
			CASE @sortDir
				WHEN 'DESC' THEN RowNumberDesc ELSE RowNumberAsc
			END AS 
			'RowNumber', [ID], Product, SecondName, Manufacturer, Category, CategoryID, DISCONTINUED
		FROM ProductSummary 
		WHERE CASE @sortDir
			WHEN 'DESC' THEN RowNumberDesc ELSE RowNumberAsc
		END
		BETWEEN @firstRow AND @lastRow
		ORDER BY 		
			CASE @sortDir 
				WHEN 'DESC' THEN RowNumberDesc 
			ELSE 
				RowNumberAsc 
			END
			
		ENDso if I add another parameter such as "secondValueCondition" then I want this to be included here (but optionally being executed):
FROM v_ProdsSummary p
		WHERE (@columnName IS NULL AND @columnValue IS NULL)
		OR
			CASE @columnName
				WHEN 'Category' THEN p.CATEGORY
				WHEN 'Manufacturer' THEN p.MANUFACTURER
				WHEN 'Manufacturer Name' THEN p.MANUFACTURER
				WHEN 'Second Name' THEN p.SECONDNAME
				WHEN 'Common Name' THEN p.SECONDNAME
				WHEN 'Product Name' THEN p.PRODUCT				
			END
		LIKE @columnValue Doing a first-pass to get the PKeys into a
 Doing a first-pass to get the PKeys into a