例如:
PROCEDURE ghxz( p_dept_id IN NUMBER,
p_user_id IN NUMBER) ISbegin
select * from aa
where user_id = p_user_id
and dept_id = p_dept_id
……
我想根据p_dept_id参数判定“and dept_id = p_dept_id这句话要不要”
例如p_dept_id=0我就不要这句话,否则要
但是如何实现呢?
请教高手们
PROCEDURE ghxz( p_dept_id IN NUMBER,
p_user_id IN NUMBER) ISbegin
select * from aa
where user_id = p_user_id
and dept_id = p_dept_id
……
我想根据p_dept_id参数判定“and dept_id = p_dept_id这句话要不要”
例如p_dept_id=0我就不要这句话,否则要
但是如何实现呢?
请教高手们
p_user_id IN NUMBER) IS begin
select * from aa
where user_id = p_user_id
and (dept_id = p_dept_id or p_dept_id = 0)
p_user_id IN NUMBER) IS begin
if p_dept_id = 0 then
select * from aa
where user_id = p_user_id;
else
select * from aa
where user_id = p_user_id
and dept_id = p_dept_id;
end if;
......这是最简单最实用的方法了,不排除还有别的方法,不过都是吃力不讨好的方法。
3楼的方法也不错
看不懂加上(dept_id = p_dept_id or p_dept_id = 0)的意义是什么?
3楼的方法可行,但是要是SQL语句非常多的话,那if else中的东西就重复性的东西就太多了
case when我听说只能写在select里?无法写在where条件语句中?我不太清楚
我的解决办法是
PROCEDURE ghxz( p_dept_id IN NUMBER,
p_user_id IN NUMBER) IS begin
select * from aa
where user_id = p_user_id
and ((p_dept_id = -1 and 1=1) or (p_dept_id<>-1 and dept_id = p_dept_id ))
(1=1也可以不写,为了便于理解)