例如:
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我就不要这句话,否则要
但是如何实现呢?
请教高手们

解决方案 »

  1.   

    PROCEDURE ghxz( p_dept_id    IN NUMBER, 
                    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)
      

  2.   

    谢谢楼上大哥,不过不是这个意思,我是想把dept_id = p_dept_id这句话当作活的语句,看p_dept_id是否=0来判定是否显示这个句话
      

  3.   

    PROCEDURE ghxz( p_dept_id    IN NUMBER, 
                    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;
    ......这是最简单最实用的方法了,不排除还有别的方法,不过都是吃力不讨好的方法。
      

  4.   

    可以用case when  来拼接条件语句。
     
    3楼的方法也不错 
      

  5.   


    看不懂加上(dept_id = p_dept_id or p_dept_id = 0)的意义是什么?
      

  6.   

    谢谢几位大哥帮忙,我已经解决了。
    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也可以不写,为了便于理解)