Optional Parameter in a SQL Function -- selecting all

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantebothermy
    New Member
    • Nov 2008
    • 47

    Optional Parameter in a SQL Function -- selecting all

    Hi, I need some help.

    I have a function with a parameter that runs a query. I'd like the parameter to be optional, so that if the user enters nothing or "all" the query will select all records.

    Is there something I can do to my WHERE clause so that it will select all records based on the value of my parameter?


    (Actually, the real function has three parameters, but the issue is easier to explain as if there's only one.)

    I'm baffled. Thanks a lot,


    Dante
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Assign NULL as the default value to your parameter. Use this technique on your WHERE clause

    Code:
    SELECT...FROM...
    WHERE yourfiltercolumn = isnull(@parameter, yourfiltercolumn)

    -- CK

    Comment

    • dantebothermy
      New Member
      • Nov 2008
      • 47

      #3
      Thanks CK! It worked

      Comment

      Working...