基于条件的where子句SQL Server存储过程 [英] Condition based where clause SQL Server stored procedure
本文介绍了基于条件的where子句SQL Server存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以建议如何在存储过程的 WHERE
子句中添加条件吗?
Can someone suggest how to add a condition in WHERE
clause of my stored procedure?
CREATE Procedure getAllEmployeesByDeptAndFlag
@Dept int,
@sal int,
@Flag int
AS
if @flag = 1
select *
from employee
where Department = @dept and @sal < 10000
else
select *
from employee
where Department = @dept
有什么方法可以简化上述过程?
Is there any way to simplify above procedure?
推荐答案
您可以使用或逻辑运算符来统一
if
语句的两个分支:
You could use the or
logical operator to unify both branches of the if
statement:
select * from employee where Department = @dept AND (@flag != 1 OR @sal < 10000)
这篇关于基于条件的where子句SQL Server存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文