0

I have a store procedure with many conditions but one of them depend of the value of one of my parameter. At the moment I'm using if statement and duplicate query, I want to improve but I can't find other way. some idea how

MyStoreProcedure Code

If @paramstate = 'All'
    Select mytable.field1, mytable.field2, mytable.field3, mytable.field4,mytable.field5
    from mytable
    Where mytable.field6 is null
    and mytable.field1 is not null
Else
    Select mytable.field1, mytable.field2, mytable.field3, mytable.field4,mytable.field5
    from mytable
    Where mytable.field6 is null
    and mytable.field1 is not null
    and mytable.state = @paramstate 
End

how can I improve it?

1 Answer 1

1

I think this removes the need for the if/else but beware that this may not be efficient

 Select mytable.field1, mytable.field2, mytable.field3, mytable.field4,mytable.field5
    from mytable
    Where mytable.field6 is null
    and mytable.field1 is not null
    and (mytable.state = @paramstate  or @paramstate = 'All')
Sign up to request clarification or add additional context in comments.

Comments

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.