0

I have UI where a user can search on optional parameters.I am writing a SP and as a testing database I am using AdventureWorks.

For some reasons I dont seem to find a way how to include

Where ModifiedDate between ''' + @Fromdate + ''' And '''+ @ToDate + '''')

in my existing SP.Can you help?

My sp looks like this

 ALTER PROCEDURE SearchPerson_Dynamic
(
@FirstName NVARCHAR(50) = NULL, 
@LastName NVARCHAR(50) = NULL, 
@EmailAddress NVARCHAR(50) = NULL, 
@Phone NVARCHAR(25) = NULL,
@FromDate DATETIME =NULL,
@ToDate DATETIME =NULL

AS
DECLARE @sSQL NVARCHAR(MAX), @Where NVARCHAR(1000) = ''
SET @sSQL = 'SELECT * FROM [Person].[Contact]'

IF @FirstName is not null
SET @Where = @Where + 'AND FirstName = @_FirstName '
IF @LastName is not null
SET @Where = @Where + 'AND LastName = @_LastName '
IF @EmailAddress IS NOT NULL
SET @Where = @Where + 'AND EmailAddress = @_EmailAddress '
IF @Phone IS NOT NULL
SET @Where = @Where + 'AND Phone = @_Phone '

-- NEED TO INTEGRATE A SEARCH BETWEEN @FromDate and @ToDate 

IF LEN(@Where) > 0
SET @sSQL = @sSQL + 'WHERE ' + RIGHT(@Where, LEN(@Where)-3)

 EXEC sp_executesql @sSQL,
    N'@_FirstName VARCHAR(50), 
            @_LastName VARCHAR(50), 
            @_EmailAddress VARCHAR(50), 
            @_Phone NVARCHAR(25)',  
    @_FirstName = @FirstName, 
            @_LastName = @LastName, 
            @_EmailAddress = EmailAddress,
            @_Phone = @Phone
            MISSING @FROMDate and @ToDate  --HOW DO i DO IT HERE

One thing I have not done is that some fields EG Surname the user can select "ALL" in the UI from the drop down. How would I implement a search where I dont have an exact parameter?

Thanks again for your help

2
  • Always worth reading Erland Sommarskog's articles on dynamic search, if you haven't before: sommarskog.se/dyn-search.html. Also, what problem are you encountering with adding the @FromDate and @ToDate to the search? Commented Nov 10, 2010 at 7:51
  • Thanks for your reply. Is it Correct to do If @FromDate is not null and @ToDate is not null Set where =@where + 'ModifiedDate' between ''' + @Fromdate + ''' And '''+ @ToDate + '''') Also in the executeSql do I simply add @_FromDate=@FromDate,@_ToDate =@ToDate Commented Nov 10, 2010 at 8:49

1 Answer 1

3

If I was to rewrite your stored proc, I would do the following

ALTER PROCEDURE SearchPerson_Dynamic
(
@FirstName NVARCHAR(50) = NULL, 
@LastName NVARCHAR(50) = NULL, 
@EmailAddress NVARCHAR(50) = NULL, 
@Phone NVARCHAR(25) = NULL,
@FromDate DATETIME =NULL,
@ToDate DATETIME =NULL
)
as
begin

    SELECT * FROM [Person].[Contact]
    where
        FirstName = coalesce(@FirstName, FirstName) and
        LastName = coalesce(@LastName, LastName) and
        EmailAddress = coalesce(@EmailAddress, EmailAddress) and
        Phone = coalesce(@Phone, Phone) and
        ModifiedDate >= coalesce(@FromDate, FromDate) and
        ModifiedDate <= coalesce(@ToDate, ToDate)
end

This is better as its not executing text for the stored procedure and could be optimized.

If you want to use the dynamic sql, you could cast the date to a normal string, ie

IF @FromDate IS NOT NULL AND @ToDate IS NOT NULL
    SET @Where = @Where + 'AND ModifiedDate between ''' + cast( @FromDate as varchar(50) ) +  ''' and  ''' +  cast( @ToDate as varchar(50) ) +  ''' '
Sign up to request clarification or add additional context in comments.

5 Comments

Such query will most likely do table scan.
Quite true, unless you put in OPTION (RECOMPILE). FYI, there a real good article on this at sommarskog.se/dyn-search.html
are you saying if you put Option(recompile) it will not do a table scan?
@user231465 - when specifying the option (recompile), the compiler will see
didnt finish my comment : @user231465 - when specifying the option (recompile), the compiler will see the fields your using cause it gets compiled each time its run and then use the proper indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.