Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2003
    Posts
    233

    Unanswered: Change sproc where clause based on parameter null

    I have an optional param passed to my sproc @Pid if that is populated i want to have it part of where clause, if it is null then not in where

    I know this is wrong, and very new to sqlsrver:

    WHERE (Pos = 'T') AND (ofDate= @OfDate)
    IF not @PID is null
    AND (PID = @PID )
    GROUP BY
    bla bla bla


    Thanks

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    WHERE  (something = @var
     OR     @var IS NULL)
    George
    Home | Blog

  3. #3
    Join Date
    Oct 2003
    Posts
    233
    Quote Originally Posted by georgev
    Code:
    WHERE  (something = @var
     OR     @var IS NULL)

    so if the var is null in "something = @var" It wont return anything, but
    becuase the var is null this " OR @var IS NULL)" will return all

    is that correct?

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I usually do that the other way with @Var is Null as the first clause.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    There's no short circuiting in SQL. Are you just talking about legibility?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by mikezx10
    so if the var is null in "something = @var" It wont return anything, but
    becuase the var is null this " OR @var IS NULL)" will return all

    is that correct?
    On the button
    George
    Home | Blog

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Ok - I talked poop. Why did I not think short circuiting occured?

    And the Mentor became the Student We all knew it would happen sooner rather than later.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  9. #9
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Don't be so hard on yourself poots!

    Remember, he who flumps first, flumps longest
    George
    Home | Blog

  10. #10
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    but the student had it wrong to begin with, so lets call this another american victory over the English.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  11. #11
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    or perhaps an average poster over the moderator gurus? is that better?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  12. #12
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Sean, you've lost all of us now...

    ...confusion is a good victory tactic; it works for me!
    George
    Home | Blog

  13. #13
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    So are you going to ban him geeV or should I?
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •