Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 930935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:25:09+00:00 2026-05-15T20:25:09+00:00

I wrote a stored procedure with optional parameters. CREATE PROCEDURE dbo.GetActiveEmployee @startTime DATETIME=NULL, @endTime

  • 0

I wrote a stored procedure with optional parameters.

 CREATE PROCEDURE dbo.GetActiveEmployee
   @startTime DATETIME=NULL,
   @endTime   DATETIME=NULL
 AS
   SET NOCOUNT ON

   SELECT columns
   FROM table
   WHERE (@startTime is NULL or table.StartTime >= @startTime) AND
         (@endTIme is NULL or table.EndTime <= @endTime)

I’m wondering whether indexes on StartTime and EndTime will be used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:25:10+00:00Added an answer on May 15, 2026 at 8:25 pm

    Yes they will be used (well probably, check the execution plan – but I do know that the optional-ness of your parameters shouldn’t make any difference)

    If you are having performance problems with your query then it might be a result of parameter sniffing. Try the following variation of your stored procedure and see if it makes any difference:

    CREATE PROCEDURE dbo.GetActiveEmployee
        @startTime DATETIME=NULL,
        @endTime   DATETIME=NULL
    AS
        SET NOCOUNT ON
    
        DECLARE @startTimeCopy DATETIME
        DECLARE @endTimeCopy DATETIME
        set @startTimeCopy = @startTime
        set @endTimeCopy = @endTime
    
        SELECT columns
        FROM table
        WHERE (@startTimeCopy is NULL or table.StartTime >= @startTimeCopy) AND
             (@endTimeCopy is NULL or table.EndTime <= @endTimeCopy)
    

    This disables parameter sniffing (SQL server using the actual values passed to the SP to optimise it) – In the past I’ve fixed some weird performance issues doing this – I still can’t satisfactorily explain why however.

    Another thing that you might want to try is splitting your query into several different statements depending on the NULL-ness of your parameters:

    IF @startTime is NULL
    BEGIN
        IF @endTime IS NULL
            SELECT columns FROM table
        ELSE
            SELECT columns FROM table WHERE table.EndTime <= @endTime
    END    
    ELSE
        IF @endTime IS NULL
            SELECT columns FROM table WHERE table.StartTime >= @startTime
        ELSE
            SELECT columns FROM table WHERE table.StartTime >= @startTime AND table.EndTime <= @endTime
    BEGIN
    

    This is messy, but might be worth a try if you are having problems – the reason it helps is because SQL server can only have a single execution plan per sql statement, however your statement can potentially return vastly different result sets.

    For example, if you pass in NULL and NULL you will return the entire table and the most optimal execution plan, however if you pass in a small range of dates it is more likely that a row lookup will be the most optimal execution plan.

    With this query as a single statement SQL server is forced to choose between these two options, and so the query plan is likely to be sub-optimal in certain situations. By splitting the query into several statements however SQL server can have a different execution plan in each case.

    (You could also use the exec function / dynamic SQL to achieve the same thing if you preferred)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.