I have a sp that filters records in my datagrid (ASP.NET) but the optional parameters are throwing me off as the user can select any combination of date range only, date range and status, date range and scope, date range and status and scope, status and scope, status only, scope only.

How can I best create the dynamic where clause for this. This is what I have so far.

Thanks.

VB Code:
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5.  
  6. CREATE PROCEDURE [dbo].[usp_LoansFilter] AS
  7.     @From datetime = Null,
  8.     @To datetime = Null,
  9.     @Status nvarchar (15) = Null,
  10.     @Scope nvarchar (15) = Null
  11. AS
  12. BEGIN
  13.     SET NOCOUNT ON;
  14.     SELECT
  15.         [tblProspects].ProspectID,
  16.         [tblProspects].FirstName + ', ' + [tblProspects].LastName AS [Prospect], 'For display purposes only
  17.         [tblLoans].LoanID,
  18.         [tblLoans].Status,
  19.         [tblLoans].DateSubmitted,
  20.         [tblLoans].PriceComplete,
  21.         [tblLoans].FMA,
  22.         [tblLoans].JFMA,
  23.         [tblLoans].Purpose,
  24.         [tblLoans].LoanAmount,
  25.         [tblLoans].Created,
  26.         [tblLoans].Last_Updated
  27.     FROM
  28.         tblLoans
  29.     INNER JOIN
  30.         tblProspects
  31.         ON tblLoans.ProspectID = tblProspects.ProspectID
  32.     WHERE
  33.         (([tblLoans].Created >= @From) AND ([tblLoans].Created <= @To))
  34.             AND
  35.         (([tblLoans].Last_Updated <= @From) AND ([tblLoans].Last_Updated <= @To))
  36.             AND
  37.         (([tblLoans].Status = @Status))
  38.             AND
  39.         (([tblLoans].PriceComplete = @Scope))
  40. END
  41. GO