dkilby
asked on
sql stored procedure + if statement
is it possible to have an if statement in a query - so if a value of a variable is 'yes' for example a line is added to the query ?
ie
@Variable
select * from table
where date >= getDate()
if @Variable = 'yes' then add this line of code to the query
and customerActive = 1
ie
@Variable
select * from table
where date >= getDate()
if @Variable = 'yes' then add this line of code to the query
and customerActive = 1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If @variable could ever possibly be null rather than no:
select * from table
where date >= getDate() and (@variable is null or @variable = 'n' or customerActive = 1)
select * from table
where date >= getDate() and (@variable is null or @variable = 'n' or customerActive = 1)
ASKER
thank you, there is chance of a null value so this worked perfectly
Open in new window