1

Have stored procedure some like this.

CREATE PROCEDURE test @ID @inventory bit = 0 @status nvarchar(2) = null AS BEGIN SELECT ID INVENTORY = (SELECT SUM(Inventory) FROM Option WHERE /*Inventory if 0 with if 1 without*/) STATUS WHERE --TODO Status if END 

how to create if statement when status can be null, '', 'preordered' for example: if null select all status, if '' select empty status, if some status - select some status. And need to create where condition for Inventory(with/without)

1

2 Answers 2

2

if null select all status, if '' select empty status, if some status - select some status

Just add a check for a null status and don't include any other condition in that case:

WHERE (@status IS NULL) /* ALL */ OR (@status = STATUS) /* match status */ 

If you add a condition for Inventory make sure you put the status check in parentheses. AND gets evaluated before OR so it's best to logically group the conditions so they don't get evaluated out-of-order.

Sign up to request clarification or add additional context in comments.

2 Comments

and what is code would be in /*ALL*/ and /* match case*/ bracket
@AleksP none - those are inline comments.
2
WHERE (@status IS NULL OR (@status =STATUS)) 

Excludes no records if @Status is null

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.